A free and open-source book on ZF3 for beginners

5.9. Extracting Parameters from Route

On route match, the router (top-level route class) returns some parameters: the "defaults" (parameters listed in the defaults section of routing configuration) plus any wildcard parameters extracted from URL string.

In your controller, you will often need to retrieve these parameters. We already did this in the examples above. In this section, we will give some summary.

To retrieve a parameter from the route in your controller's action method, you typically use the Params controller plugin and its fromRoute() method, which takes two arguments: the name of the parameter to retrieve and the value to return if the parameter is not present.

The fromRoute() method can also be used to retrieve all parameters at once as an array. To do that, call the fromRoute() without arguments, as shown in the example below:

// An example action.
public function someAction() 
    // Get the single 'id' parameter from route.
    $id = $this->params()->fromRoute('id', -1);
    // Get all route parameters at once as an array.
    $params = $this->params()->fromRoute();

5.9.1. Retrieving the RouteMatch and the Router Object

On route match, the router class internally creates an instance of Zend\Router\RouteMatch class, providing the methods for extracting the matched route name and parameters extracted from route. The useful methods of the RouteMatch class are listed in table 5.3:

Table 5.3. Zend\Router\RouteMatch class methods
Method Name Description
getMatchedRouteName() Gets the name of matched route.
getParams() Get all parameters.
getParam($name, $default) Get a specific parameter.

In most cases, it will be sufficient to use the Params controller plugin, but alternatively you can use the RouteMatch object for accomplishing the same task.

To get the RouteMatch object from your controller's action method, you can use the following code:

// An example action.
public function someAction() 
    // Get the RouteMatch object.
    $routeMatch = $this->getEvent()->getRouteMatch();
    // Get matched route's name.
    $routeName = $routeMatch->getMatchedRouteName();
    // Get all route parameters at once as an array.
    $params = $routeMatch->getParams();

In line 5 of the code above, we use the getEvent() method of the AbstractActionController base class to retrieve the MvcEvent object, which represents the event (in ZF3, the application life cycle consists of events). We then use the getRouteMatch() method of the MvcEvent class to retrieve the RouteMatch object.

In line 8, we use the getMatchedRouteName() method to retrieve the name of the route that matched the HTTP request, and in line 11 we retrieve all the parameters from the route.

The MvcEvent class can also be used for retrieving the router (the top-level route class). You can do this with the getRouter() method of the MvcEvent class, as below:

    // Call this inside of your action method
    // to retrieve the RouteStackInterface for the router class.
    $router = $this->getEvent()->getRouter();

In the code above, we use the getRouter() method, which returns the RouteStackInterface interface. This interface is the base interface for both SimpleRouteStack and TreeRouteStack, and it provides the methods for working with the routes contained inside the route stack.