Tips to improve your Routing in Laravel

Sharing is caring!

219 Views

Laravel routes

Routing is one of the most important parts of Laravel.Routing in Laravel defines which URL redirect to which action in a controller.

All Laravel routes are declared in your route files, that are placed within the routes directory routes/web.php.

Available Router Methods

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

 

you can access the following route by navigating to http://your-webapp.test/user in your browser:

Route::get('/user', 'UserController@index');

 

Sometimes you will need to define a route that responds to multiple HTTP methods. You can do that by using the match method.

Route::match(['get', 'post'], '/', function () {
//
});

 

Or, you will even register a route that responds to any or all HTTP methods using any method:

Route::any('/', function () {
//
});

 

Redirect Routes

To redirects to some another URI you can also use a route, you may use the Route::redirect method.

Route::redirect('/here', '/there');

 

View Routes

For just returning a view you can also use the route, you may use the Route::view method.

Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

 

Route Parameters

Required Parameters
Sometimes you may need to capture segments of the URI inside your route.

Route::get('student/{id}', function ($id) {
return 'Student '.$id;
});

 

Optional Parameters

If you are using an optional parameter in your route. You can use that by placing a ? mark after the parameter name. But Make sure to give the route’s optional variable a default value:

Route::get('user/{name?}', function ($name = null) {
return $name;
});

 

Named Routes

Named routes generate URLs or redirects for specific routes. You can define a name for a route by chaining the name method onto the route definition:

Route::get('user/profile', function () {
//
})->name('profile');

 

You can also specify route names for controller actions:

Route::get('user/profile', 'UserProfileController@show')->name('profile');

 

Route Groups

Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route.
Middleware

To assign a middleware to all routes for a group, you can use the middleware method before defining the group. Middleware are executed in the order they are defined in the array:

Route::middleware(['first', 'second'])->group(function () {
  Route::get('/', function () {
    // Uses first & second Middleware
  });

  Route::get('user/profile', function () {
    // Uses first & second Middleware
   });
});

 

Namespaces

Another simple use-case for route groups is assigning the same PHP namespace to a group of controllers using the namespace method:

Route::namespace('Admin')->group(function () {
// Controllers Within The "App\Http\Controllers\Admin" Namespace
});

 

Sub-Domain Routing

Route groups will also be used to handle sub-domain routing.

Route::domain('{account}.myapp.com')->group(function () {
   Route::get('user/{id}', function ($account, $id) {
    //
   });
});

 

Leave a Reply

avatar
  Subscribe  
Notify of