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

Proposal: Mapping path prefixes to middleware stacks #6

Closed
franzliedke opened this issue Mar 18, 2018 · 6 comments
Closed

Proposal: Mapping path prefixes to middleware stacks #6

franzliedke opened this issue Mar 18, 2018 · 6 comments

Comments

@franzliedke
Copy link

I will happily implement this and send a pull request, if you agree with the idea.

What do you think about something like this?

$broker->paths([
  '/foo' => $myMiddlewareStack,
  '/bar' => MyMiddleware::class,
  '/baz' => 'baz.middleware.stack'
]);

Array values can be middleware, or strings which would then be lazily resolved from the container, whenever a request with this prefix arrives.

This method would have to take care of stripping the path prefix from the request object.

@franzliedke
Copy link
Author

A little bit of background: If it had this functionality, I would gladly use this library in Flarum in order to use PSR-15 middleware. We're currently using Zend's Stratigility, but their PSR-15-compatible version does not support PHP 7.0, which is - currently - still a requirement on our side.

@shadowhand
Copy link
Contributor

shadowhand commented Mar 19, 2018

Would adding this functionality introduce new dependencies? I like the idea of it.

For whatever it's worth, the functionality is already possible by doing something like this:

$broker->when($this->matchesPath('/foo'), MyMiddleware::class);

Assuming that:

private function matchesPath(string $path): callable
{
    return static function (RequestInterface $request) use ($path): bool {
        return strpos($request->getUri()->getPath(), $path) === 0;
    };
}

@franzliedke
Copy link
Author

I would implement it without external dependencies.

Stratigility additionally strips out the path prefix from the request - which is very useful because the middleware does not need to know about the prefix. So just matchesPath is not enough.

@shadowhand Is that a "go ahead" then? 😀

@shadowhand
Copy link
Contributor

@franzliedke sure, I'd definitely consider a PR. Seems like it will be reasonable.

@franzliedke
Copy link
Author

Little status update:
I got path matching working. The only thing remaining is to change the request's URI object so that subsequent middleware does not have to know about the path prefix.

@shadowhand
Copy link
Contributor

Closing, see discussion on #7

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

No branches or pull requests

2 participants