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

Feature request: custom attributes for route and route group accessible within a middleware #301

Open
umesecke opened this issue May 20, 2021 · 6 comments
Labels

Comments

@umesecke
Copy link

I'm looking for a way to provide some kind of metadata to a route or route group so I can use that data within a middleware when working with the request that matched that route. Right now the only attributes added are the path variables that were matched with the route.

I'd like to build a single global request metrics middleware for all incoming requests where I measure request duration and export this metric to an external system (statsd_exporter/prometheus). Instead of using the actual request path as a tag/label for this metric, I'd like to categorize the requests by route (which uses path wildcards and all the stuff).

Right now I don't see a way to inject the matched route or something like this into the request attribute where I can use it inside the middleware.

Preferably I'd like to define a set of fixed attributes for a route or route group which will then be injected into the request attributes by the dispatcher.

Example:

<?php declare(strict_types=1);

$router = new League\Route\Router;

$router->group('/gallery/{id}', function (\League\Route\RouteGroup $route) {
    $route->map('GET', '/view', 'GalleryController::actionView')->setAttributes(['action' => 'view']);
    $route->map('GET', '/print', 'GalleryController::actionPrint')->setAttributes(['action' => 'print']);
})->setAttributes(['controller' => 'gallery']);

For an incoming request to "/gallery/123/view" the following request attributes would by injected:

[
    'id' => '123',
    'controller' => 'gallery',
    'action' => 'view',
]
@philipobenito
Copy link
Member

This is interesting, but because of the order middleware is invoked, it would need some thought for how best to do it.

I think the best option would be to add the matched route object itself as a request attribute then you'd pull attributes from that route object.

I need to remind myself of those internals though so leave it with me and I'll see exactly what is possible.

@umesecke
Copy link
Author

@philipobenito any news on this topic? Having this would be really nice right now but I'd prefer not to build something that is too far away from what you are thinking. Maybe you could put down your core ideas about this and I try to create a PR?

@philipobenito
Copy link
Member

Think I have a working solution for this, delay has been due to some related middleware based requests that might have been able to go in alongside this, but they won't work. I'll get something tagged next week.

@frkinta
Copy link

frkinta commented Aug 2, 2023

@philipobenito any update?
not seeing $route->setAttributes() nor $route->setVars() in documentation

@sanderdlm
Copy link

sanderdlm commented Aug 17, 2023

+1

I would love to be able to access the current Route object from the middleware stack. Right now I can't seem to find a way to get the matched callable/controller in the middlewares that run before the controller?

Would you accept a PR or do you have a working solution already locally?

@aubricient
Copy link

+1

I would love to have the proposition from @sanderdlm

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

No branches or pull requests

5 participants