简体   繁体   中英

Laravel filters in route or controllers?

Is there a best practice for where to put the filters? I want to put filter on each action for a resource (eg: user), which can look overwhelming if I put them all in routes.php , but most people do it. If I apply the filters in the controllers, I want to do that for everything else too so other people always know where to look.

How do you organize your filters?

It really doesn't matter where you put your filters (either in routes or controllers). But basically, it would be easy and understandable if you will rule your routes .

'best practice' is probably just doing it in the routes, solely because that is where most people will go looking for them.

If you want to put a filter more than one route, then then a neat way to do that is to put those routes in a Route::group .

Route::group(['before' => 'auth.whatifs'], function(){
 Route::get('user', ['uses' => 'UserController@function', 'as' => 'user.function']);
 " Another route "
 " Yet another route "
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM