php Zend Framework - 需要从视图访问 GET 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2288268/
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 05:54:10  来源:igfitidea点击:

Zend Framework - need to access a GET parameter from a view

phpzend-framework

提问by Ali

HI guys - I'm using the Zend framework and what I need is to construct a url in my view. Normally in regular php code I'd just grab the GET Variable by using the global $_GET. However with Zend I'm setting it to clean URIs so :

嗨,伙计们 - 我正在使用 Zend 框架,我需要的是在我的视图中构建一个 url。通常在常规的 php 代码中,我只是通过使用全局 $_GET 来获取 GET 变量。但是在 Zend 中,我将其设置为清理 URI,因此:

?ac=list&filter=works&page=2

?ac=list&filter=works&page=2

Looks like index/ac/list/filter/works/page/2

看起来像 index/ac/list/filter/works/page/2

In my view I'm setting a links cs such that if the GET variable filter equals works then the color of that link would be different and it would point to the same page only linked as so:

在我看来,我正在设置一个链接 cs,这样如果 GET 变量过滤器等于有效,那么该链接的颜色就会不同,它会指向同一个页面,只链接如下:

index/ac/list/filter/extra/page/2

索引/ac/list/filter/extra/page/2

ANd like wise I have a number of other links all which just one GET value - how do I set this up - Im using the Zend framework...

并且同样明智地我有许多其他链接,所有这些链接都只有一个 GET 值 - 我如何设置它 - 我使用 Zend 框架......

回答by Luiz Damim

To access a request variable direct in the view you could do:

要直接在视图中访问请求变量,您可以执行以下操作:

Zend_Controller_Front::getInstance()->getRequest()->getParam('key');

But as others have said, this is not a good idea. It may be easier, but consider other options:

但正如其他人所说,这不是一个好主意。这可能更容易,但请考虑其他选择:

  • set the view variable in the controller
  • write a view helper that pulls the variable from the request object
  • 在控制器中设置视图变量
  • 编写一个视图助手,从请求对象中提取变量

回答by alexn

If you need to access a GET parameter from a view, i think you're doing it the wrong way.

如果您需要从视图访问 GET 参数,我认为您这样做是错误的。

I suggest that you set up a route with all your parameters, and then use $this->urlfrom your view to render a valid and correct url.

我建议你用你的所有参数设置一个路由,然后$this->url从你的视图中使用 来呈现一个有效和正确的 url。

Fore som more info, check out the following blog post (no, i'm not the author): http://naneau.nl/2007/07/08/use-the-url-view-helper-please/

更多信息,请查看以下博客文章(不,我不是作者):http: //naneau.nl/2007/07/08/use-the-url-view-helper-please/

Edit:

编辑:

If you want to be 'lazy', you can set a view parameter from your controller by doing $this->view->param = $this->_getParam('param'). You can then access paramfrom your view by doing echo $this->param;. However, i do not recommend this.

如果你想变得“懒惰”,你可以通过执行从你的控制器设置一个视图参数$this->view->param = $this->_getParam('param')。然后param,您可以通过执行从您的视图访问echo $this->param;。但是,我不建议这样做。

回答by chelmertz

You can pass it in from a controller: $this->view->page = $this->_getParam('page');.

您可以从控制器传入它:$this->view->page = $this->_getParam('page');.

Footnote: I agree with @alexn.

脚注:我同意@alexn。

回答by Karthick

i am using Zend Framework v1.11 and i am doing like this

我正在使用 Zend Framework v1.11,我正在这样做

In Controller

控制器中

$this->view->request = $this->_request;

then in Viewyou can access any Request param like this

然后在视图中,您可以像这样访问任何请求参数

<h3><?= $this->request->fullname ?></h3>