2011-08-18 127 views
1

我需要得到URL的最后部分的最后一部分,同时使用Zend Framework从View(一个.phtml)Zend框架获取URL

所以我的网址目前是这样的:site.com/some/other /路径

我需要返回“路径” - 我怎么能从视图做到这一点?

回答

4

使用strrpos()找到最后的 '/' 字符串中的位置,并在其后返回的一切:

$url = 'site.com/some/other/path';  
echo substr($url, strrpos($url, '/') + 1); // Output: 'path' 

要获取URL,你可以使用:

basename($this->getRequest()->getRequestUri()); 

stated by John Cartwright

1

无论是从控制器指定一个视图变量:

$path = $this->_request->getRequestUri(); 
$parts = explode('/', $path); 
$lastPathComponent = end($parts); 

$this->view->lastPathComponent = $lastPathComponent; 

或者,如果您打算在这些会用来对多个控制器(例如,布局)视图中使用此,创建一个返回的视图助手最后一个路径组件,并从视图中调用它:

<?=$this->escape($this->lastPathComponent())?> 
1

您可以从请求对象获取url,然后将basename()应用于结果。

echo basename($this->getRequest()->getRequestUri());