You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm facing a problem of execution order caused (I suppose) by the withId method of custom actions.
It seems that when I use the withId method, the system tries to resolve the value of the ID even before executing the middleware.
Below I've attach a small code for describe the problem better. The Laravel in use is 11 and the latest version of laravel-json-api.
The middleware:
<?phpnamespaceApp\Http\Middleware;
useClosure;
useIlluminate\Http\Request;
useIlluminate\Support\Str;
useSymfony\Component\HttpFoundation\Response;
class AttachGuestId
{
publicfunctionhandle(Request$request, Closure$next): Response
{
dd("hello world"); // <- should exit here on request$guestId = $request->header('guest-session-id');
session()->set('guest_id', $guestId); // try to set a session variable
}
}
The API declaration:
// ...
JsonApiRoute::server('v1')
->prefix('v1')
->middleware('verified_or_guest', 'attach_guest_id') // <----The alias of the middleware here
->resources(function (ResourceRegistrar$server) {
$server->resource('carts', CartController::class)
->actions(function (ActionRegistrar$actions) {
$actions->withId()->post('confirm');
})->middleware('attach_session_id'); // <---- also I've try to place here
});
Now, when you try to execute GET /cart/:id/confirm the response is a 404, because it seems the global scope return no results and works before executing the middleware.
Removing the global scope declaration inside the Cart model the response is:
In this case the model was found and then the middleware was executed.
Is there any way to make sure the middleware is executing first? Thanks
The text was updated successfully, but these errors were encountered:
Hi, I'm facing a problem of execution order caused (I suppose) by the
withId
method of custom actions.It seems that when I use the
withId
method, the system tries to resolve the value of the ID even before executing the middleware.Below I've attach a small code for describe the problem better. The
Laravel
in use is11
and the latest version oflaravel-json-api
.The middleware:
The API declaration:
The method inside the controller:
The Cart model with a Global scope set
Now, when you try to execute
GET /cart/:id/confirm
the response is a 404, because it seems the global scope return no results and works before executing the middleware.Removing the global scope declaration inside the Cart model the response is:
In this case the model was found and then the middleware was executed.
Is there any way to make sure the middleware is executing first? Thanks
The text was updated successfully, but these errors were encountered: