Zend Framework 2 routing and setting the defaults 'module', 'controller', 'action' of a route

StackOverflow https://stackoverflow.com/questions/21489436

  •  05-10-2022
  •  | 
  •  

Question

I'm using Zend 2 version 2.2.5 and the Skeleton Application. I added a simple 'Member' module and inside a 'TestController.php'.

What is the best way to write the 'test' route's 'defaults' section?

And later, how do I get the 'module' name from a matched route? I expect there is a simple way in ZF2 to get 'module', 'controller', 'action', but don't know how.

Option 1: Produces a 404 Error

'defaults' => array(
  'module'     => 'Member',
  'controller' => 'Test',
  'action'     => 'index',
),

A $matchedRoute->getParam('module');     prints 'Member'
A $matchedRoute->getParam('controller'); prints 'Test'
A $matchedRoute->getParam('action');     prints 'index'

A 404 error occurred Page not found. The requested controller could not be mapped to an existing controller class. Controller: Test(resolves to invalid controller class or alias: Test)

Option 2: Works, but 'module' is empty

'defaults' => array(
  '__NAMESPACE__' => 'Member\Controller',
  'controller'    => 'Test',
  'action'        => 'index',
),

A $matchedRoute->getParam('module');     prints '' <= EMPTY
A $matchedRoute->getParam('controller'); prints 'Test'
A $matchedRoute->getParam('action');     prints 'index'

Option 3: Works, but 'module' is empty

'defaults' => array(
  'controller' => 'Member\Controller\Test',
  'action'     => 'index',
),

A $matchedRoute->getParam('module');     prints '' <= EMPTY
A $matchedRoute->getParam('controller'); prints 'Member\Controller\Test'
A $matchedRoute->getParam('action');     prints 'index'

I try do get the 'module', 'controller', 'action' in onBootstrap() with this code:

$sm = $application->getServiceManager();
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
print($matchedRoute->getParam('module'));
print($matchedRoute->getParam('controller'));
print($matchedRoute->getParam('action'));
Was it helpful?

Solution

You typically add a route for your action like below:

'test' => array(
   'type' => 'Zend\Mvc\Router\Http\Literal',
   'options' => array(
      'route'    => '/test',
      'defaults' => array(
         'module'     => '__NAMESPACE__',
         'controller' => '__NAMESPACE__\Controller\Test',
         'action'     => 'index',
      ),
    ),
  ),

To make the __NAMESPACE__ constant work, add the following line to the beginning of your module's config:

namespace Member;

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.

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 indexAction() {

  // 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();

  //...             
}

Retrieving the RouteMatch and the Router Object

On route match, the router class internally creates an instance of Zend\Mvc\Router\RouteMatch class, providing the methods for extracting the matched route name and parameters extracted from route.

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

// An example action.
public function indexAction() {

  // 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();

  //...             
}

For additional information about ZF2 routing, I would recommend reading the Using Zend Framework 2 book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top