Skip to content

Laravel 5.4: Fluent Routing

Last updated on February 17, 2018

Laravel 5.4 has released another pretty cool routing improvement. In this post, I would like to show you the improved fluent Registering Routes.

In the past, you used to define a named route like this at the end of the closure –

Route::get('projects/{id}', function ($id) {
    //
})->name('myProjects');

Now you can define it at the beginning, as shown below –

Route::name('myProjects')->get('projects/{id}', function ($id) {
// 
});

Same way you can define your middleware also, here the example-

Route::middleware('auth')->get('projects/{id}', function ($id) {
// 
});

You can chain and use together both middleware and named routes, as shown below –

middleware(‘auth’)->get(‘projects’, function () {
    // some closure action…
});

Registering a middleware with a route prefix and group

Route::middleware('auth')->prefix('api')->group(function () {
    // register some routes...
});

Registering a middleware to a resource controller

Route::middleware('auth')->resource('project', 'ProjectController');

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments