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

Handle base path by routeCollector instead of RouteCollectorProxy #2844

Merged
merged 3 commits into from Sep 25, 2019

Conversation

piotr-cz
Copy link
Contributor

@piotr-cz piotr-cz commented Sep 24, 2019

At this moment, basePath is handled by both RouteCollectorProxy and RouteCollector.

This PR:

  • respects RouteCollector::$basePath when creating routes via Dispatcher
  • changes RouteCollectorProxy::setBasePath and RouteCollectorProxy::getBasePath methods which now call RouteCollector methods
  • renames RouteCollectorProxy::$basePath to $groupPattern as that's what it is

There are no B/C changes.

Fixes #2842 and possibly #2512

For test case, see Slim-4-basepath-issue

@coveralls
Copy link

coveralls commented Sep 24, 2019

Coverage Status

Coverage remained the same at 100.0% when pulling 6912d53 on piotr-cz:bugfix/basepath-handling into b63b7d4 on slimphp:4.x.

@l0gicgate l0gicgate added this to the 4.3.0 milestone Sep 24, 2019
@l0gicgate l0gicgate merged commit d084972 into slimphp:4.x Sep 25, 2019
@piotr-cz piotr-cz deleted the bugfix/basepath-handling branch September 25, 2019 16:00
@l0gicgate l0gicgate mentioned this pull request Oct 5, 2019
@morozov
Copy link

morozov commented Oct 11, 2019

There are no B/C changes.

FWIW, there is a BC break. Prior to this PR, the base path, if set using a middleware, was only effective when generating URLs but not when routing requests. Now it's effective with routing as well.

While it may have been a bug, it could be used for in case when the Slim API sits behind a reverse proxy (see #2367).

morozov added a commit to morozov/kih that referenced this pull request Oct 11, 2019
The reasons are:

1. It has been a continuous source of troubles during Slim upgrades.
2. I may have been abusing it since I was passing the full URL there instead of just a path.
3. It broke again after slimphp/Slim#2844 was merged.
4. I don't really need it right now.
@piotr-cz
Copy link
Contributor Author

piotr-cz commented Oct 11, 2019

As I understand it, this is a bug as documentation mentions only the App::setBasePath method here.

I guess if you would like to use different base path for creating routes, easiest way would be to mutate the request URI in the middleware.

This may not be semantically correct but the trick is that once the routing phase is done mutated URI object will be used to render routes.

I'm attaching a code I've been using in custom middleware before this PR

public function process(Request $request, RequestHandler $handler): ResponseInterface
{
    $basePath = static::resolveBasePath();

    $uri = $request->getUri();
    $path = $uri->getPath();

    // Strip base (don't use ltrim)
    $path = substr($path, strlen($basePath));

    // Update request
    $request = $request->withUri($uri->withPath($path));

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

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

Successfully merging this pull request may close these issues.

4.x - Inconsistent setBasePath Behavior
5 participants