laravel-json-api icon indicating copy to clipboard operation
laravel-json-api copied to clipboard

Resorce route middleware not work

Open sogasas opened this issue 4 years ago • 7 comments

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

sogasas avatar Jun 05 '21 03:06 sogasas

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.

lindyhopchris avatar Jun 05 '21 10:06 lindyhopchris

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

sogasas avatar Jun 10 '21 14:06 sogasas

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.

lindyhopchris avatar Jun 10 '21 16:06 lindyhopchris

@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

sogasas avatar Jun 10 '21 16:06 sogasas

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...');

lindyhopchris avatar Jun 11 '21 09:06 lindyhopchris

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

sogasas avatar Jun 13 '21 15:06 sogasas

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.

lindyhopchris avatar Jun 14 '21 11:06 lindyhopchris