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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5510958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:40:20  来源:igfitidea点击:

ZF: How to get current url on any page in the view?

phpmodel-view-controllerzend-frameworkurl

提问by Anthony

I have a value in general layout. I need to set this value to current url on any page. How to do this?

我在总体布局中具有价值。我需要将此值设置为任何页面上的当前 url。这该怎么做?

Thanks to all

谢谢大家

回答by asologor

In Zend Framework 2you can use Zend\View\Helper\ServerUrl view helper. It's very simple:

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/

Learn more: http://codingexplained.com/coding/php/zend-framework/create-full-urls-in-zf2-views

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

回答by Marcin

There are few ways you could get the requested url. One is through $_SERVER["REQUEST_URI"], as you did, but off course ZF has its own ways. Two examples are:

有几种方法可以获取请求的 url。一种是通过$_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');

Hope this helps.

希望这可以帮助。

回答by refugene

In ZF1 and ZF2 you can use the Url View Helper with null to get the current route

在 ZF1 和 ZF2 中,您可以使用带有 null 的 Url View Helper 来获取当前路线

<?= $this->url() ?>

<?= $this->url() ?>