Apply a middleware to a specific method of the controller #1045
-
Hi, I am facing a problem while applying an authentication middleware to a specific route from a controller. Basically I was able to apply the middleware on the routes that start with Route::crud('route', 'controller') Now I want to apply this middleware to the setupCreateOperation method from a controller (so it doesn't open the controller/create route) but when I do it it seems to not recognize the route and doesn't apply to it. This is an example of what I want to achieve: // Users that have the type "user" are not allowed to access these routes I tried to search on the documentation for a way to break down the controller on different routes but I didn't find anything that could do it. Thanks in advance for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @algodoncin Middleware is just one stack layer before the controller so you have two options:
For the second option, you can just add into your if(backpack_user()->hasRole('user')) { // or whatever you are using to check the user type.
CRUD::denyAccess(['update', 'delete', 'show']); // this will prevent users with type "user", from accessing update, delete and show operations.
} Here are the docs for CRUD access functions: https://backpackforlaravel.com/docs/6.x/crud-api#access Let me know if that helps. Cheers |
Beta Was this translation helpful? Give feedback.
Hey @algodoncin
Middleware is just one stack layer before the controller so you have two options:
Route::crud()
and create all the routes manually, so you can apply middleware to each route.For the second option, you can just add into your
setup()
method:Here are the docs for CRUD access functions: https://backpackforlaravel.com/docs/6.x/crud-api#access
Let me know if tha…