Laravel Route Filters

Laravel Filters: Route access depending on the user group

Filters are one of the Larevel’s features that I like the most. It’s pretty straightforward – you need to run some chunk of code before or after some action. Recently I had to implement restrictions on route resources depending on user group. I use Sentry for user authentication and role management and this Laravel filter should check if user belongs to specified group and allow him access to route resource if he does.

One solution is to simply create a new filter for every group. But, I don’t like repetition. I wanted simple and elegant solution that meets my requirements, without unnecessary repetition of code. Here is solution I came up with:

Now, you just need to add filter ‘inGroup:Editor;Admin’ to route or route group you want and only users with specified Sentry groups can access to these resources. In this case we allow access only to users from Editor and Admin groups:

Or if you want to use only on one route and allow access to users from Admin and SuperAdmin groups:

For more information about laravel filters, you can check official documentation.

 

 

Emir Softić
Follow me

Emir Softić

Web Developer
Emir Softić
Follow me

Latest posts by Emir Softić (see all)

Published by

Emir Softić

Web Developer