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

后端 未结 5 2746
梦谈多话
梦谈多话 2021-02-20 16:00

I am 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 w

相关标签:
5条回答
  • 2021-02-20 16:26

    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
    0 讨论(0)
  • 2021-02-20 16:39

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

    In Controller

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

    then in View you can access any Request param like this

    <h3><?= $this->request->fullname ?></h3>
    
    0 讨论(0)
  • 2021-02-20 16:46

    To access the Request Object one way that is common is to save it in the Registry.

    1. http://osdir.com/ml/php.zend.framework.mvc/2007-08/msg00158.html
    2. http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-access-request-object-customizing-layout-view-3349.html
    0 讨论(0)
  • 2021-02-20 16:48

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

    I suggest that you set up a route with all your parameters, and then use $this->url from your view to render a valid and correct 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/

    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 param from your view by doing echo $this->param;. However, i do not recommend this.

    0 讨论(0)
  • 2021-02-20 16:51

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

    Footnote: I agree with @alexn.

    0 讨论(0)
提交回复
热议问题