Issues

ZF2-149: Route match does not provide matched parameters (e.g. in segment)

Description

Steps to reproduce * Create a new SkeletonApplication * Add a new route to Application module:

         'test' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route'    => '/test[/:id]',
                'defaults' => array(
                    'controller' => 'index',
                    'action'     => 'index',
                    'id'        => 0
                ),
            ),
        ),
  • Modify IndexController::indexAction to retrieve the parameter "id"

* echo $id and exit
* Create a new request to http:///test/1

Expected result:
The number 1 should be displayed

Actual result:
Nothing is displayed

Additional information:
It looks like Zend\Mvc\Router\SimpleRouteStack and inheritors ignore the accumulated request assembledParams in a matched Route\Segment.

Proposed solution:
Add

to Zend\Mvc\Router\SimpleRouteStack line 267 (in match() method, after the foreach loop ends) Note that this same solution may apply to Zend\Mvc\Router\TreeRouteStack for applications with a baseUrl

Comments

Althought it would be a simple way to extract them, you can extract them right now if your controller implements the InjectApplicationEvent, then you can retrieve the MVC Event, and extract the route matches:


$Match = $this->getEvent()->getRouteMatch();
$Id = $Match->getParam('id', 0);

The proposed solution breaks other functionality: existing params in the query are lost. The following change merges both params: $request->setQuery(new \Zend\Stdlib\Parameters(array_merge($match->getParams(), $request->query()->toArray())));

It would be great if somebody from zf could comment on this.

Route parameters are not query parameters, so this issue is basically invalid. What is correct is that we had Request::getParam() before, which combined all parameter sources (user params, get, post, cookie, etc.). This may return at a later point, but is not related to this issue. So right now you would do:


$this->getRequest()->getRouteMatch()->getParam('id');

What you probably meant is:


$this->getEvent()->getRouteMatch()->getParam('id');