Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation unclear with before and after middleware #596

Closed
scorgn opened this issue Oct 22, 2021 · 1 comment · Fixed by #723
Closed

Documentation unclear with before and after middleware #596

scorgn opened this issue Oct 22, 2021 · 1 comment · Fixed by #723

Comments

@scorgn
Copy link

scorgn commented Oct 22, 2021

The middleware concepts page for Slim documentation is not exactly clear on the difference between before and after middleware.

On lines 5-9 it says that you can have middleware complete before and after the Slim application, and that before middleware would be useful to protect from cross-site request forgery.

Then on lines 73-81 it shows an example of two different middleware callables, one being $beforeMiddleware and the after being $afterMiddleware.

I think that this gives off the impression that the $beforeMiddleware callable is the same type of middleware that is described as middleware that would execute before the Slim application runs. The $beforeMiddleware callable is actually an "after middleware" though, because the first thing it does in the callable is handle the request.

I know that the behavior of the middleware is defined in PSR-15 but it may be good to clarify how to perform middleware logic before the application runs and how to perform middleware logic after the application runs. An example of a middleware that short-circuits the middleware flow and returns a response without calling $handler->handle() may be good to have as well.

@odan
Copy link
Contributor

odan commented Oct 22, 2021

I think you are right. The $beforeMiddleware is actually an outgoing middleware because it handles things after the handle method. I agree, it is confusing and should be changed as follows:

$beforeMiddleware = function (Request $request, RequestHandler $handler) {
    $authorization = $request->getHeaderLine('Authorization');

    // Validate bearer token
    // If invalid, throw a HttpForbiddenException
    // ...

    return $handler->handle($request);
};

or this:

$beforeMiddleware = function (Request $request, RequestHandler $handler) {
    $csrfToken = $request->getHeaderLine('X-CSRF-Token');

    // Validate CSRF token
    // If invalid, throw a HttpForbiddenException
    // ...

    return $handler->handle($request);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants