Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Middleware can be used before and after routes in Laravel 5 to perform tasks such as authentication, permission checks, data modification, and more.

To use middleware before routes in Laravel 5, you can add the middleware to the middleware array in the route definition:

Route::get('/dashboard', 'DashboardController@index')->middleware('auth');

In this example, the 'auth' middleware will be called before the DashboardController@index method is executed. The 'auth' middleware can be defined in the $routeMiddleware array in the app/Http/Kernel.php file.

To use middleware after routes in Laravel 5, you can define a middleware group in the kernel that includes the middleware and the web middleware, and then apply the group to the routes:

Route::group(['middleware' => ['web', 'log']], function () {
    Route::get('/dashboard', 'DashboardController@index');
});

In this example, the 'log' middleware will be called after the DashboardController@index method is executed. The 'web' middleware is required for sessions and CSRF protection. The 'log' middleware can be defined in the $middlewareGroups array in the app/Http/Kernel.php file.