3

How can we access route, post, get, server parameters from VIEW file in ZF2 way?

Here I found the almost same question but no where mentioned about view nor answered anywhere

Thanks

Community
  • 1
  • 1
Sanju
  • 401
  • 3
  • 7
  • 15
  • Is there a concrete Use-Case? Usually you'd pass what you need to the View. It's not the Views responsibility to know about these kind of things. If you need Query params to be attached to the URL, there's a parameter for that inside `$this->url(` – Sam Apr 15 '13 at 07:53
  • 2
    or you could write decent code and not put that in your view file..that's really NOT the place to be manipulating those. It's simple enough to pass them to your view. – Andrew Apr 15 '13 at 08:01
  • `in ZF2 way` you do **not** access that info from the view. – Matteo Tassinari Apr 15 '13 at 09:03
  • gotcha! Thanks for the right suggestion – Sanju Apr 15 '13 at 11:01

2 Answers2

11

You have to create a view helper to fetch these parameters for you.

View Helper

Simply copy over the Zend\Mvc\Controller\Plugin\Params to App\View\Helper\Params and make a few adjustments:

<?php

namespace App\View\Helper;

use Zend\Mvc\MvcEvent;
use Zend\Stdlib\RequestInterface;
use Zend\View\Helper\AbstractHelper;

class Params extends AbstractHelper
{
    protected $request;

    protected $event;

    public function __construct(RequestInterface $request, MvcEvent $event)
    {
        $this->request = $request;
        $this->event = $event;
    }

    public function fromPost($param = null, $default = null)
    {
        if ($param === null)
        {
            return $this->request->getPost($param, $default)->toArray();
        }

        return $this->request->getPost($param, $default);
    }

    public function fromRoute($param = null, $default = null)
    {
        if ($param === null)
        {
            return $this->event->getRouteMatch()->getParams();
        }

        return $this->event->getRouteMatch()->getParam($param, $default);
    }
}

Just replace all instances of $controller with the $request and $event properties. You get the idea. (Don't forget to copy over the DocBlock comments!)

Factory

Next we need a factory to create an instance of our view helper. Use something like the following in your App\Module class:

<?php
namespace App;

use App\View\Helper;
use Zend\ServiceManager\ServiceLocatorInterface;

class Module
{
    public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                'Params' => function (ServiceLocatorInterface $helpers)
                {
                    $services = $helpers->getServiceLocator();
                    $app = $services->get('Application');
                    return new Helper\Params($app->getRequest(), $app->getMvcEvent());
                }
            ),
        );
    }
}

How to use

Once you have all this you're in the home stretch. Simply call up the params view helper from within your view:

// views/app/index/index.phtml
<?= $this->params('controller') ?>
<?= $this->params()->fromQuery('wut') ?>

Hope this answers your question! Let me know if you need any clarifications.

radnan
  • 1,279
  • 10
  • 10
1

I created Params View Helper for this purpose (as @radnan suggested).

Install it via composer

composer require tasmaniski/zf2-params-helper

Register new module

'modules' => array(
    '...',
    'ParamsHelper'
),

And use it

$this->params()->fromPost();  //read all variables from $_POST
$this->params()->fromRoute(); //read all variables from Routes
$this->params()->fromQuery(); //read all variables from $_GET

Take a look in full documentation GitHub source

tasmaniski
  • 4,767
  • 3
  • 33
  • 65