views:

313

answers:

4

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 :

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

Looks like 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:

index/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...

+4  A: 

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.

alexn
Interesting but it doesn't say much on accessing existing GET parameters..
Ali
@Ali, yes it does. yourdomain.com/news/id/124/page/2 <-- $_GET['id'] will be accessed as `$this->_getParam('id')` in the controller, $_GET['page'] is $this->_getParam('page');
chelmertz
I mean I need to access GET parameters in my view... :(
Ali
No, you should not need to do that. Tell us why and we're may be able to give you a solution.
alexn
A: 

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

Footnote: I agree with @alexn.

chelmertz
+1  A: 

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
Luiz Damim