ZF:如何在视图中的任何页面上获取当前URL?

Ant*_*ony 17 php url model-view-controller zend-framework

我在总体布局中有一个值.我需要在任何页面上将此值设置为当前网址.这该怎么做?

谢谢大家

aso*_*gor 40

Zend Framework 2中,您可以使用Zend\View\Helper\ServerUrl视图助手.这很简单:

//for example, your current URI is: http://mydomain.com/page/sample/

//get your domain
$uri = $this->serverUrl(); // Output: http://mydomain.com

//get your current URI
$uri = $this->serverUrl(true); // Output: http://mydomain.com/page/sample/
Run Code Online (Sandbox Code Playgroud)

了解更多信息:http://codingexplained.com/coding/php/zend-framework/create-full-urls-in-zf2-views

  • ZF1也有这个实现.看看Zend_View_Helper_ServerUrl,它也是如此. (2认同)

Mar*_*cin 37

您可以通过几种方式获取请求的网址.一个是通过$_SERVER["REQUEST_URI"],就像你一样,但当然ZF有自己的方式.两个例子是:

  // you can call it in a view or a layout
  $uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();

  // or using userAgent view helper in a view or a layout:
  $uri = $this->userAgent()->getServerValue('request_uri');
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.