2011-08-18 143 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());