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

Global Middleware Pipeline as in Mezzio #17

Open
boesing opened this issue Sep 17, 2021 · 0 comments
Open

Global Middleware Pipeline as in Mezzio #17

boesing opened this issue Sep 17, 2021 · 0 comments

Comments

@boesing
Copy link
Member

boesing commented Sep 17, 2021

Feature Request

Q A
New Feature yes
RFC yes
BC Break no

Summary

We recently had some discussion in slack and also had a use-case in a project inside our company.
The most common use-case is a global authentication middleware which verifies that a request is authenticated.

So instead of writing an MVC dispatch listener (which would not be forward compatible for a probably upcoming mezzio migration - if thats something a project wants to achieve) in each project, there could be an MVC listener within this component to allow projects to specify a "global pipeline".

In mezzio, one is talking about "project pipeline" and "route pipeline". As of now, this component only allows specifying "route pipelines" only so each route has to add the authentication middleware repeatedly.

Having a global pipeline in a project would allow projects to move the authentication middleware to that pipeline.


I personally was thinking about two ways of doing so (I actually dont have a real preferation):

Global Middleware Pipeline for All Requests (Containing Those Routes Which Are Not Explicitly Using the Pipespec Controller)

This would require a dedicated DispatchListener which then passes the request through the global middleware pipeline.

This scenario can be quite annoying as it would be hilariously hard to detect if any middleware/pipeline created a response or if the "final" handler was reached and thus, anything is "fine".

Global Middleware Pipeline for Only Pipespec Requests (Leaving Out Those Which Do Implement Old Actioncontroller-Style Logic)

This would be actually my preferred way as it only applies for requests which do make use of this component and therefore could be implemented in the HandlerFromPipeSpecFactory#createPipeFromSpec-method.

This would also be very clear about how to handle requests as the middleware actually MUST return a PSR-7 response while the "all requests" global pipeline would need some dedicated project config to detect if the dispatch event must be stopped and the response from the global middleware has to be returned of if the dispatch event may bubble to the next listener.


Troubleshooting

Mezzio DispatchMiddleware

A global middleware pipeline in a mezzio (or other PSR-15) project probably looks like this:

[
    ErrorHandlerMiddleware::class,
    RouteMiddleware::class,
    WhateverMiddleware::class,
    DispatchMiddleware::class,
    NotFoundHandler::class, // <- final and only called if RouteMiddleware did not provide RouteResult to DispatchMiddleware
]

This is actually not really achievable when using a global middleware pipeline for all requests as a usual MVC Controller does not provide a PSR-7 response and is also executed later in the "pipeline" (dispatch event priority).

So in the end, one has to somehow pass the pipeline with the event to handle the event after dispatch was finished for those endpoints which do not implement the PipeSpec and thus returning responses and/or views and needs rendering.


Happy to have some discussion here to see how we can achieve this.

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

No branches or pull requests

1 participant