2

说,我有一个 URL http://server/mysite/web/app_dev.php/resource/1。我正在做一个GET请求,相应的操作是ResourceController::getAction.

在这个控制器动作中,如果我打电话$request->getPathInfo(),它会给我/resource/1

但是在同一个控制器中,如果我使用另一个资源的 url 创建一个 Request 对象并调用getPathInfo()它会返回一个更长的版本。

$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

只有在这种情况下才能getPathInfo()退货?/another_resource/1

或者

任何人都可以建议在 Symfony2 中将端点 URL 转换为最安全的方法是什么http://server/mysite/web/app_dev.php/another_resource/1/another_resource/1

如果你有兴趣知道我为什么需要这个

控制器操作正在接收请求内容中的一些 URL。该操作需要解析这些 URL 以识别相应的资源。我正在尝试使用$router->match函数从 URL 中检索参数。match 函数只需要/another_resource/1部分。

4

1 回答 1

0

Request::create 方法创建一个不知道有关当前请求的任何信息的新请求,它返回完整的 uri,因为它不知道当前的脚本文件。尝试:

$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();
于 2015-09-29T19:00:38.393 回答