问题
Say, I have an URL http://server/mysite/web/app_dev.php/resource/1
.
I am doing a GET
request and the corresponding action is ResourceController::getAction
.
In this controller action if I call $request->getPathInfo()
, it gives me /resource/1
.
But in the same controller if I create a Request object with a url of another resource and call getPathInfo()
it returns a longer version.
$request = Request::create('http://server/mysite/web/app_dev.php/another_resource/1');
echo $request->getPathInfo();
OUTPUT >>
/mysite/web/app_dev.php/another_resource/1
How is it possible to make getPathInfo()
to return only /another_resource/1
in this case?
OR
Anyone can suggest what is the safest way to convert an endpoint URL http://server/mysite/web/app_dev.php/another_resource/1
to /another_resource/1
in Symfony2?
In case you are interested to know why I need this
The controller action is receiving some URLs in request content. The action needs to parse those URLs to recognize the corresponding resource. I am trying to make use to $router->match
function to retrieve the parameters from the URL. The match function expects only /another_resource/1
part.
回答1:
Request::create method create a new request that don't know any information about current request, it return full uri because it not know current script file. try:
$request = Request::create('http://server/mysite/web/app_dev.php/another_resource/1', null, array(), array(), array(), array(
'SCRIPT_NAME' => $this->get('kernel')->getEnvironment() == 'dev' ? 'app_dev.php' : 'app.php',
'SCRIPT_FILENAME' => $this->get('kernel')->getEnvironment() == 'dev' ? 'app_dev.php' : 'app.php',
));
echo $request->getPathInfo();
来源:https://stackoverflow.com/questions/32808325/symfony2-get-path-info-of-url-explicitely