我正在使用$request->getPathInfo()
. 但是,检索到的 url 始终是 prod 环境的 url,这意味着我在测试期间无意中从 dev 环境移动到了 prod 环境。
有没有办法确保返回正确的 URI(即与浏览器窗口中显示的相同)?
我的操作代码如下所示:
public function executeSomeAction(sfWebRequest $request)
{
$this->url = $request->getPathInfo();
}
我正在使用$request->getPathInfo()
. 但是,检索到的 url 始终是 prod 环境的 url,这意味着我在测试期间无意中从 dev 环境移动到了 prod 环境。
有没有办法确保返回正确的 URI(即与浏览器窗口中显示的相同)?
我的操作代码如下所示:
public function executeSomeAction(sfWebRequest $request)
{
$this->url = $request->getPathInfo();
}
正如你在里面看到的sfWebRequest.class.php
,从 url中getPathInfo()
删除了:$relativeUrlRoot
$pathInfo = $pathArray[$sf_path_info_key];
if ($relativeUrlRoot = $this->getRelativeUrlRoot())
{
$pathInfo = preg_replace('/^'.str_replace('/', '\\/', $relativeUrlRoot).'\//', '', $pathInfo);
}
$relativeUrlRoot
来自这个函数,它从 url 中删除控制器名称。
public function getRelativeUrlRoot()
{
if (null === $this->relativeUrlRoot)
{
if (!($this->relativeUrlRoot = $this->getOption('relative_url_root')))
{
$this->relativeUrlRoot = preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName());
}
}
return $this->relativeUrlRoot;
}
所以如果你有myapp_dev.php/main
或index.php/main
,你总会得到/main
。
如果您想要之前的完整路径/main
,您还必须调用$this->request->getScriptName()
.
小心,getScriptName()
将返回域和路径信息之间的所有内容。如果有http://exemple.com/somepath/app_dev.php/main
,getScriptName()
将返回:/somepath/app_dev.php
。
如果您想要完整的 URI 以及地址可能具有的任何查询字符串,您可以使用
$request->getUri();
这将返回浏览器地址栏中可见的整个地址。