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

Get requestpath with variables #26

Closed
lucacastelnuovo opened this issue May 21, 2020 · 4 comments
Closed

Get requestpath with variables #26

lucacastelnuovo opened this issue May 21, 2020 · 4 comments

Comments

@lucacastelnuovo
Copy link

Would it be possible to have an $request->getUri() alternative that returns the base path.

Example:

  1. Request to the route /example/{var} with /example/123
  2. $request->getUri() returns /example/123

How would I get the original base (/example/{var})? Maybe $request->getBase()?

@miladrahimi
Copy link
Owner

Currently, this feature is no available. Could you please explain why do you need this feature and what's your use case?

@lucacastelnuovo
Copy link
Author

I'm trying to create ratelimit middleware (https://github.com/Luca-Castelnuovo/CubeQuence-framework/blob/master/src/CubeQuence/Middleware/RateLimit.php). And I would like to be able to have multiple limits set for multiple endpoints.

For the following example:

$router->get('/foo', 'ExampleController@foo', RateLimit::class); // 45 requests / 60 seconds
$router->get('/bar', 'ExampleController@bar', RateLimit::class); // 10 requests / 5 seconds

I was planning to achieve this by getting the request path

$path = $request->getUri();
$path = strtok($path, '?');
$path = strtok($path, '#');

And then lookup aan associative array

$path = Request::path($request);

$this->max_requests = (int) Config::get("ratelimit.{$path}.max_requests") ?: 60;
$this->reset_time = (int) Config::get("ratelimit.{$path}.reset_time") ?: 60;

This would work fine if I didn't use URL params. Because if you use the following:

$router->get('/fooBar/{uuid}', 'ExampleController@foo', RateLimit::class); // 45 requests / 60 seconds

I wouldn't be able to create an config entry for each uuid.
So if I could get the basepath like:

$basePath = Request::basePath($request);

$this->max_requests = (int) Config::get("ratelimit.{$basePath}.max_requests") ?: 60;
$this->reset_time = (int) Config::get("ratelimit.{$basePath}.reset_time") ?: 60;

I could configure limits for each route.

@miladrahimi
Copy link
Owner

Thanks for your suggestion, I'm working on the next major version of PhpRouter, I will consider this feature.

@miladrahimi
Copy link
Owner

This feature is added to the last release. Please check:
https://github.com/miladrahimi/phprouter#current-route

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