Seer
Seer

Reputation: 5237

How to get current relative URL in Symfony2 service?

Pretty much as simple as the title says. Really what I mean is how can I get the current relative URL, including the file name if there is one, i.e everything emphasised below:

http://hostname/app/app_dev.php/path/to/some/action?even=a%20query%20string
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Now, this is easy enough if you don't need the file, you get the RequestStack, get the current request and use something like this in a service:

$request->getPathInfo();

But in my current example within this question that would bring back the following:

/path/to/some/action?even=a%20query%20string

Without the /app_dev.php, which is the important part for me currently. The other alternative I have already exhausted is using the router to generate the current route, which does work nicely in some ways, it does show the file name, but there are some other issues in that if some part of the query string that isn't defined in the routing is included in the current request then it wouldn't show up in the result from this.

So, let's pretend in my above example again that the even query string wasn't included in the routing, I couldn't generate that and this method would just return something like this:

/app_dev.php/path/to/some/action

Which is of course, still not ideal.

So, how can I go about getting everything I'm looking for here, reliably?

Upvotes: 3

Views: 6768

Answers (4)

Raja Ram T
Raja Ram T

Reputation: 9071

It's possible using RequestStack , i used this way in my service

just inject the request_stack, which behaves like any normal service.

class ExServ {


    private $requestStack;

    public function __construct($requestStack) {

        $this->requestStack = $requestStack;

    }

    public function tesFun() {


        $relativeURL = $this->requestStack->getMasterRequest()->getRequestUri();
$absoluteURL = $this->requestStack->getMasterRequest()->getUri();

}

}

Upvotes: 1

Seer
Seer

Reputation: 5237

This was actually a lot simpler than I thought. All I had to use was:

$request->getRequestUri();

And it returns exactly what I was looking for.

Upvotes: 7

Javad
Javad

Reputation: 4397

You can check what's the Symfony environment is as @sjagr says; but if you still need to get the section of full path without checking Symfony environment you can do something like:

//After you send RequestStack to your service and assume you assign it to $this->request
$url = $this->Request->getUri();
$pieces = parse_url($url);
$looking_for_section = substr($pieces['path'], strrpos(strstr($pieces['path'], '.', true), '/')) . $pieces['query'];

Hopefully it helps you

Upvotes: 3

sjagr
sjagr

Reputation: 16502

Knowing your motives would be useful. If you absolutely need to get the script name, you could use the $_SERVER superglobal: $_SERVER['SCRIPT_NAME'] which will give you the relative path to the bootstrap file (in this case, /path/to/app_dev.php.) You can use string manipulation to just get the last part.

But you'll notice that app_dev.php is different from app.php because of this line:

$kernel = new AppKernel('dev', true);

You can easily check to see if Symfony is in the dev environment which would tell you that you are in fact using app_dev.php.

If you want to check the request URI to see if the end-user is trying to access the bootstrapper directly instead of through the .htaccess routes, you can use $_SERVER['REQUEST_URI'] but you'd have to use some manipulation to get rid of the base URL and subdirectory (where the bootstrapper lives.)

To get a combination of everything (quick code, forgive me if there's any mistakes or bad practice):

$scriptPath = explode('/', $_SERVER['SCRIPT_NAME']);
$relUrl = end($scriptPath).$_SERVER['PATH_INFO'].(!empty($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : '');

Upvotes: 0

Related Questions