Laravel Route

Laravel Route
Written by Sagar RabidasDecember 13, 2021
7 min read
Laravel
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Sagar Rabidas

Software Developer

In this blog, we will discuss Laravel Route.

Introduction to Laravel Route

The laravel course institution lets in the developer to course all the groups. This helps in developing clean codes and whoever takes over the development of the website will be able to comply with the precise recurring. The other blessings one receives with grouping the routes is that each one of the attributes can be shared with all the corporations. This saves plenty of time. In every other framework, this might should be completed in my opinion. However, with the laravel framework, this feature seems like a pressure multiplier. Route grouping additionally saves duplication which otherwise would have been the case if done in my view. Attributes like middleware and namespaces are allowed to be shared in a collection without doing it individually.

Syntax

The query looks like this:

Route::group( [ ] , callback);

Explanation:

Use of Parameters

below are some parameters:

Code:

Route::get('/page/{number}', function ($number) {
echo "Your are on page ". $number;
});

Use of parameters that are optional and with values that are default:

Code:

Route::get('/page/{number?}', function ($number = 1) {
echo "Your are on page ". $number;
});

Using expressions in place for parameters meant for routing:

Code:

Route::get('/page/{number?}', function ($number = 1) {
echo "Your are on page ". $number;
})->where('number', '[0-9]+');

The naming of the routes:

Code:

Route::post('/submit', 'ContactFormController@submitForm')->name('contact.submit');

Grouping the routes:

Code:

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});

Another form of the grouping of the routes can look like this:

Code:

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::group(['middleware' => ['auth']], function () {
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});
});

The usage of namespaces:

Code:

Route::group(['namespace' => 'Post'])
// this route group will load all controllers
//from within the "App\Http\Controllers\Post"
Route::group(['namespace' => 'Post','prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::group(['middleware' => ['auth']], function () {
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});
});

If one has to route the cache:

PHP artisan route: clear
PHP artisan route: cache

And finally, if the route has to be debugged:

PHP artisan route: list

Another quick example for route grouping:

Code:

// In Laravel 4.0 your routes would look something like this
Route::get('/', array('as' => 'home', 'uses' => 'Controllers\HomeController@index'));
Route::get('admin/dashboard', array('as' => 'admin.dashboard.index', 'uses' => 'Controllers\Admin\DashboardController@index'));
// etc. This gets messy very quickly.
// Laravel 4.1 allows us to write this a lot cleaner:
Route::group(array('namespace' => 'Controllers'), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'))
Route::group(array('namespace' => 'Admin'), function()
{
// Notice how, by nesting route groups, the namespace will automatically
// be nested as well!
Route::get('admin/dashboard', array('as' => 'admin.dashboard.index', 'uses' => 'DashboardController@index'));
});
});

Examples to Implement Laravel Route

Let us see some examples mentioned:

Example #1

Code:

Route::group([], function()
{
Route::get('/first',function()
{
echo "first route";
});
Route::get('/second',function()
{
echo "second route";
});
Route::get('/third',function()
{
echo "third route";
});
});

Example #2

Code:

Route::group(['prefix' => 'tutorial'], function()
{
Route::get('/aws',function()
{
echo "aws tutorial";
});
Route::get('/jira',function()
{
echo "jira tutorial";
});
Route::get('/testng',function()
{
echo "testng tutorial";
});
});

Example #3

Code:

Route::middleware(['age'])->group( function()
{
Route::get('/aws',function()
{
echo "aws tutorial";
});
Route::get('/jira',function()
{
echo "jira tutorial";
});
Route::get('/testng',function()
{
echo "testng tutorial";
});
});
<?php
namespace App\Http\Middleware;
use Closure;
class CheckAge
{
/**
* Handle an incoming request.
*
* @param
\Illuminate\Http\Request  $request
* @param  \Closure  $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//return "middleware";
echo "Hello javaTpoint <br>";
return $next($request);
}
}.

Example #4

Code:

127.0.0.1       localhost
127.0.0.1       fakebook.dev
127.0.0.1       masud.fakebook.dev
127.0.0.1       sohel.fakebook.dev
::1             localhost
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\blog\public"
ServerName fakebook.dev
<directory "C:\xampp\htdocs\blog\public">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny, Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
Route::group(['domain' => 'fakebook.dev'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
Route::group(['domain' => '{username}.fakebook.dev'], function()
{
Route::any('/', function($username)
{
return 'You visit your account: '. $username;
});
});

Conclusion

The laravel framework with evocative and expressive from querying allows the developer in developing scalable features that can resist the pains of current-day programming. The path grouping is an exquisite manner to preserve the code neat in addition to lessen programming time.

blackchain
hyperledger fabric
hyperledger
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block
Sagar Rabidas
Software Developer
157 Blog Posts
15 Discussion Threads
Trending Categories
15
Software39
DevOps45
Frontend Development24
Backend Development18
Server Administration17
Linux Administration24
Data Center24
Sentry24
Terraform21
Ansible29
Docker28
Penetration Testing14
Kubernetes16
NGINX19
JenkinsX17
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan