Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Resourceful Routes Plus Middleware

Is it possible to add middleware to all or some items of a resourceful route?

For example...

<?php  Route::resource('quotes', 'QuotesController'); 

Furthermore, if possible, I wanted to make all routes aside from index and show use the auth middleware. Or would this be something that needs to be done within the controller?

like image 221
kilrizzy Avatar asked Feb 25 '15 20:02

kilrizzy


2 Answers

In QuotesController constructor you can then use:

$this->middleware('auth', ['except' => ['index','show']]); 

Reference: Controller middleware in Laravel 5

like image 75
Marcin Nabiałek Avatar answered Sep 20 '22 12:09

Marcin Nabiałek


You could use Route Group coupled with Middleware concept: http://laravel.com/docs/master/routing

Route::group(['middleware' => 'auth'], function() {     Route::resource('todo', 'TodoController', ['only' => ['index']]); }); 
like image 35
Thomas Chemineau Avatar answered Sep 21 '22 12:09

Thomas Chemineau