Thanks to visit codestin.com
Credit goes to github.com

Skip to content

$request->getUri(); adds an extra (wrong) slash #8963

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

Closed
cmfcmf opened this issue Sep 9, 2013 · 11 comments
Closed

$request->getUri(); adds an extra (wrong) slash #8963

cmfcmf opened this issue Sep 9, 2013 · 11 comments

Comments

@cmfcmf
Copy link
Contributor

cmfcmf commented Sep 9, 2013

I downloaded the Symfony Standard Edition 2.3.4 and placed the following code in app_dev.php:

echo $request->getUri();

Then I visited the following url: http://localhost/RSA-Programm/web/app_dev.php?module=search&type=user&func=form. The output is http://localhost/RSA-Programm/web/app_dev.php/?func=form&module=search&type=user. Notice the additional slash after app_dev.php.

Refs zikula/core#1094. The same problem occurs in Zikula. Routing is not yet enabled so all urls are like the one above (with index.php). This causes the url in the profiler to be wrong:
image

@fabpot
Copy link
Member

fabpot commented Sep 9, 2013

That's not a bug. /app_dev.php?... does not make sense as there is no path. The minimal path you can ask for is / which is the root path for a website.

@fabpot fabpot closed this as completed Sep 9, 2013
@ghost
Copy link

ghost commented Sep 9, 2013

@fabpot What if you are not using short URLs?

@fabpot
Copy link
Member

fabpot commented Sep 9, 2013

What do you mean? The shortest resource you can ask for in HTTP is /.

@ghost
Copy link

ghost commented Sep 9, 2013

If you are using short URLs then there are no files in the URI, only directories, so slashes are ok. But in this case there is no URL rewriting, the URIs are index.php?arg1=1&arg2=2.. etc.

@fabpot
Copy link
Member

fabpot commented Sep 9, 2013

I don't understand. Is index.php the Symfony front controller? If yes, then we need a path, which is / if empty like in your example. If no, then it's not related to Symfony.

@ghost
Copy link

ghost commented Sep 9, 2013

@fabpot yes it's a front controllers, that assumes that index.php is a default if no file specified. What if it was admin.php??

@fabpot
Copy link
Member

fabpot commented Sep 9, 2013

The name does not change anything. When you type http://example.com in a browser, the request that is sent is http://example.com/:

GET / HTTP/1.1
Host: example.com

You cannot have an empty path.

@ghost
Copy link

ghost commented Sep 9, 2013

If I browse a sf controller directly as http://localhost/src/index.php then $request->getUri(); returns http://localhost/core13/src/index.php/ - surely it should return http://localhost/core13/src/index.php? That's easy to adjust for but http://localhost/core13/src/index.php?foo=bar returns http://localhost/core13/src/index.php/?foo=bar

@ghost
Copy link

ghost commented Sep 9, 2013

The above scenario can be avoided by comparing REQUEST_URI and SCRIPT_NAME for example to determine if a trailing slash is required (ie not when the URI ends with an actual file).

http://localhost/src/

REQUEST_URI     /src/
SCRIPT_NAME /src/index.php

http://localhost/src/index.php

REQUEST_URI     /src/index.php
SCRIPT_NAME /src/index.php

If a trailing slash is added incorrectly as in /src/index.php/?foo=bar it causes any assets on the page with a relative link, say an image to images/lamp.png to be searched for as /src/index.php/images/lamp.png` and therefor will result in a 404.

@Tobion
Copy link
Contributor

Tobion commented Sep 9, 2013

duplicate of #6634

@fabpot for /app_dev.php? the path is /app_dev.php. the pathinfo which is after the front controller (not defined in the http spec) should IMO be the empty string. otherwise symfony will return a different path from what was requested. this is problematic for calculating the relative path for example. but see my issue.

@ghost
Copy link

ghost commented Aug 20, 2019

@fabpot Thank you for making Symfony such a great framework! I love Symfony and Symfony components and use Symfony a lot for my non-Symfony projects. I guess the original author @cmfcmf was trying to address the same issue as I have run into! I added symfony/http-foundation component to my legacy PHP project, which means this specific project is not based on any kind of PHP framework, just plain PHP. My composer.json looks like this:

{
    "version": "1.2",
    "require": {
        "php": ">=5.5.12",
        "symfony/yaml": "~3.4",
        "symfony/process": "~3.4",
        "symfony/http-foundation": "3.3.0",
    }
}

My project is running on PHP 5.5.12 so I had to use symfony/http-foundation: 3.3.0.

When I use Request::getUri() method to get the current page's URL, it adds an extra slash like this:

Correct Page URL: http://myproject.local/www/cp/reporting.php?startreport=1
URL via getUri() method: http://myproject.local/www/cp/reporting.php/?startreport=1

That extra slash caused a lot of problems to me. For now I had to remove that slash using the following code.

$currentUrl = preg_replace('#\.php/+#i', '.php', $request->getUri(), 1);

Obviously I don't like this code :) I guess it's something to deal with PATH_INFO.

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

3 participants