Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resorce route middleware not work #611

Open
sogasas opened this issue Jun 5, 2021 · 7 comments
Open

Resorce route middleware not work #611

sogasas opened this issue Jun 5, 2021 · 7 comments

Comments

@sogasas
Copy link

sogasas commented Jun 5, 2021

Hi for averyone

Am trying to add a middleware to one of my resource routes but apparently it can't access the middleware when I enter the route:

Some help

Screen Shot 2021-06-04 at 10 56 43 PM

@lindyhopchris
Copy link
Member

apparently it can't access the middleware

what is the error? your message doesn't give enough info on the problem to determine what the cause is.

Worth noting that if it can't load your own custom middleware, that's far more likely to be an issue with your application than this package.

@sogasas
Copy link
Author

sogasas commented Jun 10, 2021

hi @lindyhopchris

thx for your response.

how much the route is accessed, the middleware should be called first, right? but it accesses the route without making the call

@lindyhopchris
Copy link
Member

What middleware is listed when you use php artisan route:list (obviously filtering it by the route that has the problem)? That should confirm whether or not the middleware has been correctly added, as it should be shown in the middleware list for that route.

@sogasas
Copy link
Author

sogasas commented Jun 10, 2021

@lindyhopchris sure

ok. in the first image, i have the middleware added to route, and the next image, i have the route list, i dont see the middleware added to this route, just i see sanctum middleware but this is in header route group.

Screen Shot 2021-06-10 at 11 50 15 AM

Screen Shot 2021-06-10 at 11 53 10 AM

if i add this middleware to route group, it work.

the middleware is 'role_or_permission' from spatie roles and permissions package

@lindyhopchris
Copy link
Member

So I think your problem is this:

$resource->get('/')

That will collide with the JSON:API index route for the resource (i.e. the route that lists all resources for that resource type). The routes() helper method is for registering additional routes, not ones that collide with the routes that the package registers to match the JSON:API spec.

To add that middleware to the resource routes registered by the package, use:

$api->resource('bloodTypes')
  ->controller('BloodTypeController')
  ->middleware('role_or_permission...');

@sogasas
Copy link
Author

sogasas commented Jun 13, 2021

Hi @lindyhopchris

ok, but i dont need assign the route to group route, i need assign to single resource, in the official documentation we have an example, but, this not work, if i put the middleware assign before resource, not work too

Screen Shot 2021-06-13 at 10 13 34 AM

@lindyhopchris
Copy link
Member

Your use is different though to the example. This won't work:

$resource->get('/')

Because it collides with the JSON:API routes registered for the resource. The example uses a custom route i.e. one that doesn't collide with the JSON:API routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants