开发者

Zend Framework Get Last Part of URL

I need to get the last part of the URL while using Zend Framework from开发者_运维百科 a View (.phtml)

So my URL currently is something like: site.com/some/other/path

I need to return "path" -- how can I do this from a view?


Use strrpos() to find the position of the last '/' in the string, and return everything after it:

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

To get the URL, you can use:

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

as stated by John Cartwright.


Either assign a view variable from the controller:

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

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

Or, if you are going to use this in a view that's used for multiple controllers (e.g. a layout), create a view helper that returns the last path component, and call it from the view:

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


You can get the url from the request object, then apply basename() to the result.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜