4

I am new to PHP frameworks and building REST API on Zend Framework 2. I want to add params to Request. I could not find method to add params so I'll do it by getting all params then, adding new params to them and then setting this new set of params to Request. I get params using

$this->params()->fromQuery()

but, I don't find any way to set params back to Request. Is there any method available for this?

EDIT : I tried below. Which is not giving desired result.

In Module.php :

public function onBootstrap(\Zend\Mvc\MvcEvent $e)
{
    $em = $e->getApplication()->getEventManager();
    echo "Outside";

    $em->attach (MvcEvent::EVENT_DISPATCH, function(MvcEvent $e) {

        echo "Inside";
        $routeMatch = $e->getRouteMatch();
        $routeMatch->setParam("myParam", "paramValue");        
    });
} 

In my controller :

echo "myParam : " . $this->params()->fromQuery('myParam');

Param value is null when I get it. This is on account of controller code is executed first(where I get param value) and then the Dispatch event is triggered(where I add param to RouteMatch).

Outside
myParam : 
Inside
Geek
  • 8,280
  • 17
  • 73
  • 137

3 Answers3

3

Probably you don't need to add params to the request, and you can achieve the same adding params to the routeMatch (this is, the object tha represents the route that the current requests matches). I do this every now and then.

I use to do it in the Module.php, in the onBootstrap function. UPDATE: Attaching some code to the EVENT_ROUTE

public function onBootstrap(MvcEvent $e) {

 $em = $e->getApplication ()->getEventManager ();
 $em->attach ( MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {

         $routeMatch   = $e->getRouteMatch();

         $params =  //your params, as an array

    foreach($params as $key => $value)  

       $routeMatch->setParam($k, $v);

    });
}
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
  • I have passed params in request. So it is stored in some array and I can get them using `$this->params()->fromQuery('param')`. Now, I want to add other params to same array so that I can get them using same method `fromQuery()`. – Geek Dec 31 '13 at 05:21
  • actually the params from the request, will be in the routematch. if you add some params later, as im doing, and somewhere else you retrieve the routematch again, you will be able to read also your new params. so i guess is in practice the same – Carlos Robles Jan 03 '14 at 00:44
  • `$e->getRouteMatch();` returns `null` value. – Geek Jan 03 '14 at 05:01
  • sorry, i omitted two much. you have to do it in a callback to the dipatch event, when routematch is aleready defined. i updated the answer – Carlos Robles Jan 03 '14 at 10:21
  • Thank you for edit. I still don't get param value. Please see my edit for complete description. – Geek Jan 03 '14 at 11:48
  • if using routematch, in the controller, instead of ->params(), you have to use `$routematch=$this->getEvent()->getRouteMatch(); $routematch->getParam($key)'` – Carlos Robles Jan 03 '14 at 15:36
  • You actually misunderstood what happens. The problem is that the code in controller is executed first and then `Dispatch` event is triggered. i.e. code to get param value executes prior to code to set param to route match. – Geek Jan 04 '14 at 04:50
  • you are right. You have to assign the parameters after the MvcEvent::EVENT_ROUTE event. I updated the code. PLease give it a try. If i can think of something better i will be back – Carlos Robles Jan 05 '14 at 03:59
0

Querystring parameters are just simple key-value pairs and they are properties of the Http Uri instance, not Request's itself directly. You can use the public api of the \Zend\Uri\Http instance which implements UriInterface to modify them.

For example, in your controller you can:

/**
 * @var \Zend\Uri\Http Implements UriInterface
 */
 $uri = $this->getRequest()->getUri();

 $params = $uri->getQueryAsArray();

 // Remove a parameter by name
 unset($params['baz']);

 // Add a key-value pair
 $params['foo'] = 'bar';

 $uri->setQuery($params);

But this may not a good practice if we talking about a REST api. There are multiple ways to interact with qs parameters, params() is just a controller plugin and makes our lives easier.

edigu
  • 9,878
  • 5
  • 57
  • 80
0
public function onBootstrap(MvcEvent $e)
{
    $em = $e->getApplication()->getEventManager();
    $em->attach(MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
        $request = $e->getRequest();
        $request->getQuery()->set('myParam', 'paramValue');
    });
}

Untestet, but this should work (play around with different MvcEvent constants if it does not).

See: https://github.com/zendframework/zf2/blob/release-2.2.5/library/Zend/Http/Request.php#L225 $request->getQuery() returns and object of \Zend\Stdlib\ParametersInterface (https://github.com/zendframework/zf2/blob/release-2.2.5/library/Zend/Stdlib/ParametersInterface.php)/ParametersInterface.php

BreyndotEchse
  • 2,192
  • 14
  • 20