Closed feryardiant closed 8 years ago
Well Given that we use https://github.com/nikic/FastRoute ... I am not 100% sure.
This is not a bug. I somewhat think its a browser/web server behaviour. I just tried this on some websites and it worked, also note some websites do redirect to the url with only 1 slash.
https://github.com///slimphp/Slim/issues/1679 https://www.google.com///doodles
@silentworks yes, you're right. But, can you see the difference between common behaviour and the way slim does?
I've also expect that slim route should work that way in my setup.
Request URI | Response |
---|---|
example.com// |
render home template |
example.com//foo |
render hallo template, with $args['name'] = 'foo' |
example.com//foo/bar |
return not found |
example.com//foo/bar/bas |
return not found |
example.com///foo |
render hallo template, with $args['name'] = 'foo' |
example.com///foo/bar |
return not found |
example.com///foo/bar/bas |
return not found |
Not to be a stickler.... but difference between common behaviour and the way slim does
... we are using a 3rd party router that is very popular ... See my last comment.
So, anyone have test this case with other framework that use same 3rd party router that is very popular??
This feels like a bug to me.
I'm looking at the //foo/bar
case.
This comes into Slim as $_SERVER['REQUEST_URI']
which is set to //foo/bar
as you'd expect.
However, Uri::createFromEnvironment
extracts it using this line:
$requestUri = parse_url($env->get('REQUEST_URI'), PHP_URL_PATH);
which results in $requestUri
set to /bar
This isn't what I would expect!
I'm pretty sure that using parse_url
is wrong in this case and this line should be:
$requestUri = $env->get('REQUEST_URI');
Thoughts @codeguy, @silentworks?
This is not a bug and is documented in FastRoute.
From the README.md of FastRoute
// Matches /user/foobar, but not /user/foo/bar
$r->addRoute('GET', '/user/{name}', 'handler');
// Matches /user/foo/bar as well
$r->addRoute('GET', '/user/{name:.+}', 'handler');
The correct pattern would be... $app->get('/[{name:.+}]', 'App\Actions\HomeAction:index')->setName('home-page');
example.com// ==> home example.com//foo ==> hallo example.com//foo/bar => matches because of the .+ example.com//foo/bar/bas => matches because of the .+ example.com///foo => hallo example.com///foo/bar => matches because of the .+ example.com///foo/bar/bas => matches because of the .+
I am not a regex wizard. But there would be a regex possible I think to do exactly what he wants.
Also as a follow-up @akrabat
From FastRoute's documentation...
// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
This has nothing to do with FastRoute.
For the URL http://example.com//foo/bar
we send /bar
to FastRoute. i.e. we've removed the //foo/
from the URL before we set it into the Uri
instance.
That documentation snippet explains where we got the idea to use parse_url()
!
However, the documentation for parse_url says:
Note: This function doesn't work with relative URLs.
haha...
This function is intended specifically for the purpose of parsing URLs and not URIs.
How about solving like this:
$requestUri = parse_url('http://example.com' . $env->get('REQUEST_URI'), PHP_URL_PATH);
As we only want the URI, the actual value of the scheme and domain are unimportant, but it will make parse_uri
work as we would expect.
That solution looks fine to me.
I was wondering if the output of requestURI was being based into the constructor of the URI but it is not.
:+1:
Here I have setup my
routes.php
and here my index method in action class
and sure it works as expected
example.com[/]
home
templateexample.com/foobar
hallo
template, with$args['name'] = 'foobar'
example.com/foo/bar
but I don't expect it could work like this.
example.com//
home
templateexample.com//foo
home
templateexample.com//foo/bar
hallo
template, with$args['name'] = 'bar'
example.com//foo/bar/bas
example.com///foo
home
templateexample.com///foo/bar
home
templateexample.com///foo/bar/bas
home
templateIs it a bug?
EDIT: I'm using local ubuntu server with nginx