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'));