sparkmix
sparkmix

Reputation: 2499

zend expressive - middleware checks if there is a next one available?

In my routes.global.php I've this one in routes.

    [
        'name' => 'test',
        'path' => '/test',
        'middleware' => [App\Action\Test1Action::class, App\Action\Test2Action::class],
        'allowed_methods' => ['GET'],
    ],

And I've this return $next($request, new JsonResponse($data)); at the end of Test1Action class so it will send the data to the next action.

But is there a way inside Test1Action to check if there's another action after?

Maybe there's another way so I can do the above return if there is one after or return the json response rite away.

return new JsonResponse($data);

That way I can either use Test1Action alone or plug it in before other action.

I tried few options but didn't work. Any help will be great. Thanks.

Upvotes: 0

Views: 96

Answers (1)

user2408230
user2408230

Reputation:

Maybe you can check $next if it's null or not. But it might always be set, I've never tried it. However in Expressive 2 it's always set and it changes to Delegates. Also the last middleware will always be the NotFoundHandler.

Since you develop your application yourself you know the order of middleware and Actions. I would let Test1Action middleware do it's thing, add the result to the $request as an attribute and let the next middleware figure out if the data was set or not. If the data wasn't set than skip it and execute the next middleware in line. It makes it a lot easier.

Upvotes: 1

Related Questions