Reputation:
I've looked at the Laravel code and their documentation recommends setting the middleware in the corresponding controller, but speaks nothing about setting it on multiple routes?
It seems a very bad way to do it the way they describe, is there a better way which allows adding it to many routes from one part? Or at least defining the middleware in the web.php but prefer former.
At the moment I have to set it in the controller
Upvotes: 1
Views: 904
Reputation:
You can either make a group and put all your routes inside that group, or you can assign the middleware in the web.php, I would opt for the latter for multiple routes.
As long as your routes are inside the group, they should all follow the rules of that group, IE the middleware that you have set when declaring the group.
You can add it to a specific route in web.php:
Route::get('/', 'LandingController@index')->middleware('guest');
Or you can group multiple routes to a single middleware:
Route::group(['middleware' => 'guest'], function() {
Route::get('/', 'LandingController@index');
Route::get('/welcome', 'WelcomeController@index');
Route::post('/welcome', 'WelcomeController@index');
});
You can also assign multiple mws in a group:
Route::group(['middleware' => ['mw1', 'mw2', 'mw3']], function() {
Route::get('/', 'LandingController@index');
Route::get('/welcome', 'WelcomeController@index');
Route::post('/welcome', 'WelcomeController@index');
});
Upvotes: 2
Reputation: 1807
You can use Route::group and set your middleware there
Route:group(['middleware' => 'auth:web'], function() {
$this->get('/', 'HomeController@index');
$this->get('/posts', 'PostController@index');
});
Upvotes: 0