我正在使用$request->getPathInfo()
设置操作中的当前url。然而,检索到的url始终是prod环境的url,这意味着在测试过程中我不小心从dev移动到了prod环境。
是否有任何方法可以确保返回正确的URI(即与浏览器窗口中显示的URI相同)?。
我的操作代码如下:
public function executeSomeAction(sfWebRequest $request)
{
$this->url = $request->getPathInfo();
}
正如您在sfWebRequest.class.php
内部看到的,getPathInfo()
从url:中删除了$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();
这将返回浏览器地址栏中可见的整个地址。