【问题标题】:ZF: How to get current url on any page in the view?ZF:如何在视图中的任何页面上获取当前 url?
【发布时间】:2011-07-27 12:38:14
【问题描述】:

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

谢谢大家

【问题讨论】:

  • 尝试更准确地了解您想要的内容,并发布一些代码。
  • 对不起我的英语。它通过 $_SERVER["REQUEST_URI"] 暂时解决了它。我需要在视图中通过 ZF 获取 $_SERVER["REQUEST_URI"];

标签: php model-view-controller zend-framework url


【解决方案1】:

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/

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

【讨论】:

  • 是的,你是对的,但它似乎适用于 ZF2。问题是 ZF1 和 ZF2 在 2011 年不存在-)
  • @Anthony 我没看到,抱歉:) 但是我在 ZF2 中搜索如何做同样的事情时发现了你的问题:)
  • ZF1 也有这个实现。看看 Zend_View_Helper_ServerUrl,它也是一样的。
【解决方案2】:

有几种方法可以获得请求的 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');

希望这会有所帮助。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 它只返回url的一部分,没有action或其他参数。问题是关于当前网址,而不仅仅是路线。
    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 2013-09-21
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 2011-07-15
    相关资源
    最近更新 更多