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

Slim 4 basepath and request path #2898

Closed
tuupola opened this issue Dec 13, 2019 · 6 comments
Closed

Slim 4 basepath and request path #2898

tuupola opened this issue Dec 13, 2019 · 6 comments

Comments

@tuupola
Copy link
Contributor

tuupola commented Dec 13, 2019

Let's assume an app which is installed into subfolder /test and there is a route such as:

$app->get("/foo", function ($request, $response, $arguments) { ... }

Then later in a middleware or a route definition user gets the request path using:

 $request->getUri()->getPath();

With slim 3 the result is the same as the defined route ie. foo. With Slim 4 the result includes the basepath ie it will be/test/foo. Is this expected behaviour?

Example code to demonstrate the problem at:

https://github.com/tuupola/slim-basepath/tree/slim-3
https://github.com/tuupola/slim-basepath/tree/slim-4

@l0gicgate
Copy link
Member

Which PSR-7 implementation are you using with Slim 4? That’s not a problem caused within this repo, more likely in the Slim-Psr7 repo if that’s what you’re using

@tuupola
Copy link
Contributor Author

tuupola commented Dec 13, 2019

Yep, using slim/psr7. However I get same behaviour with nyholm/psr7: https://github.com/tuupola/slim-basepath/tree/slim-4-nyholm

$ make slim-4-nyholm
$ curl http://192.168.51.60/test/foo
/test/foo

@l0gicgate
Copy link
Member

Yeah, that’s because Slim-Psr7 is modeled after Nyholm/Psr7. This means Slim 3 behavior was irregular.

Slim 4 is PSR-7 agnostic, the requests are created via the respective ServerRequest creators of each lib, the internals of Slim 4 don’t change or append Uri properties.

@tuupola
Copy link
Contributor Author

tuupola commented Dec 13, 2019

Ok. In other words expected behaviour.

I guess to get routes with path matching the route definition I should either use a custom ServerRequest creator or a middleware which removes the folder from $request object. Middleware probably being easier option?

@l0gicgate
Copy link
Member

If I may suggest a test. Remove Slim entirely and use Nyholm/ps7 and Nyholm/psr7-server and call the fromGlobals() method https://github.com/Nyholm/psr7-server/blob/master/src/ServerRequestCreator.php#L45 and then call $request->getUri()->getPath() and see if you obtain the same result

@tuupola
Copy link
Contributor Author

tuupola commented Dec 13, 2019

Folder gets prefixed. Tested with following code:

use Nyholm\Psr7Server\ServerRequestCreator;
use Nyholm\Psr7\Factory\Psr17Factory;

$app->get("/globals", function ($request, $response, $arguments) {
    $request = (new ServerRequestCreator(
        new Psr17Factory(),
        new Psr17Factory(),
        new Psr17Factory(),
        new Psr17Factory()
    ))->fromGlobals();
    
    $path = $request->getUri()->getPath();
    $response->getBody()->write($path);
    return $response;
})

Yields:

$ curl http://192.168.51.60/test/globals
/test/globals

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

No branches or pull requests

2 participants