In symfony request how do I get the uri path without the query params?

Viewed 7008
$request = new Symfony\Component\HttpFoundation\Request()
$request->getRequestUri();

Seems to return the path and the query params. How to I get just the path?

4 Answers

$request->getPathInfo() is what you're looking for.

strtok($request->getRequestUri(), '?');

...or...

$request->getBaseUrl() . $request->getPathInfo();

Or you can use just PHP for that matter :

$urlWithoutQueryString = strtok($_SERVER["REQUEST_URI"], '?');

None of the above worked for me in Laravel 5.7. I ended up using the following to get the full request URI without the query string:

$uri = strtok($request->getUri(), '?');

This will return https://example.com/request/uri for a request made to https://example.com/request/uri?param=1.

Related