Search code examples
zend-framework2

How to get URL params in ViewHelper with Zend Framework 2


I've created simple ViewHelper with http://blog.evan.pro/creating-a-simple-view-helper-in-zend-framework-2. How to get URL params in this helper? $this->params('param') works only in controllers...


Solution

  • Given the code from the blog post, you can use this code from inside the view helper:

    $this->request->getPost('param');  // post parameter
    
    // or
    
    $this->request->getQuery('param'); // query parameter
    

    The code from the example receives an instance of the Zend\Http\Request object for the current request and stores it in the property called request of the view helper so you can use the request property to access the Request object and information from it.