I suggest you to use Gates and Policies ( https://laravel.com/docs/5.5/authorization ) will be more secure and convenient way to authorize your users based on roles.
middleware in controller or on route
Just wondering if there are any drawbacks to putting middleware directly on a route or route group as opposed to putting it in the constructor on a controller. The reason I ask is because a lot of apps I build have different user types e.g. members, admins, public users etc...
I generally have a single controller for a model so for example if I only wanted an admin to be able to delete a post I would need to add isAdmin middleware. If I stick that in the controller constructor my show(), index() methods etc.. would not be available to members or public. So what I have been doing on a recent project is grouping my routes and adding the middleware to the group so /admin/* has isAdmin middleware. Is this secure?
@deansatch From documentation: https://laravel.com/docs/5.5/controllers#controller-middleware
"However, it is more convenient to specify middleware within your controller's constructor. Using the middleware method from your controller's constructor, you may easily assign middleware to the controller's action. You may even restrict the middleware to only certain methods on the controller class"
I believe it is a convenience issue and not a performance issue or security issue. You have to see what works for you.
If it was a security, I believe that would be known. Also you have the "RedirectIfAuthenticated" middleware by default to check if a user is authenticated.
As for whether it is a good practice or not, I would not know. I do use it myself for permissions when it makes sense for a group of routes. I have not used gates and policies either, so I cant say.
Please or to participate in this conversation.