Zend framework 2 REST API: sets request parameters

I am new to PHP frames and am building a REST API on Zend Framework 2. I want to add params to Request. I could not find a method for adding parameters, so I will do this by getting all the parameters, adding new parameters for them, and then setting this new set of parameters Request. I get parameters using

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

but I cannot find a way to return params to Request. Is there any method for this?

EDIT: I tried below. Which does not give the 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');

null, . - , ( ), Dispatch ( RouteMatch).

Outside
myParam : 
Inside
+4
3

, , routeMatch ( , , ). .

Module.php, onBootstrap. UPDATE: 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);

    });
}
+3

Querystring - -, Http Uri, Request . public api \Zend\Uri\Http, UriInterface, .

, :

/**
 * @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);

, REST api. qs, params() - .

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, ( MvcEvent, ).

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

0
source

All Articles