laravel-json-api
laravel-json-api copied to clipboard
Resorce route middleware not work
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

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.
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
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 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.


if i add this middleware to route group, it work.
the middleware is 'role_or_permission' from spatie roles and permissions package
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...');
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

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.