The slim documentation says:
The only hard requirement is that a middleware MUST return an instance of \Psr\Http\Message\ResponseInterface
The example code shows prefixing and postfixing a message to the response body
<?php
$app = new \Slim\App();
$app->add(function ($request, $response, $next) {
$response->getBody()->write('BEFORE');
$response = $next($request, $response);
$response->getBody()->write('AFTER');
return $response;
});
$app->get('/', function ($request, $response, $args) {
$response->getBody()->write(' Hello ');
return $response;
});
$app->run();
Given the the response object must be returned, how would you go about implementing a middleware component to prevent the Hello being displayed from the home page route above.
For example, letâs say I have a route /resource and I want to ensure that only consumers who have a âcorrectâ Authentication: header set can reach the resource, I could put a check in the middleware.
What is not clear to me is how the middleware signals to the main app->get function that authentication has failed and not to display the resource. Presumably, the middleware canât break the call chain as its contract says âMUSTâ return a response.