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 3 commits into from Sep 25, 2019


Copy link

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


This comment has been minimized.

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 the Slim 4 label Sep 24, 2019
@l0gicgate l0gicgate added this to the 4.3.0 milestone Sep 24, 2019
@l0gicgate l0gicgate requested a review from adriansuter Sep 24, 2019
@l0gicgate l0gicgate merged commit d084972 into slimphp:4.x Sep 25, 2019
1 of 2 checks passed
1 of 2 checks passed
continuous-integration/travis-ci/pr The Travis CI build is in progress
coverage/coveralls Coverage remained the same at 100.0%
@piotr-cz piotr-cz deleted the piotr-cz:bugfix/basepath-handling branch Sep 25, 2019
@l0gicgate l0gicgate referenced this pull request Oct 5, 2019

This comment has been minimized.

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.

This comment has been minimized.

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
None yet
5 participants
You can’t perform that action at this time.