Zend Framework 2 routing and setting the defaults 'module', 'controller', 'action' of a route -
i'm using zend 2 version 2.2.5 , skeleton application. added simple 'member' module , inside 'testcontroller.php'.
what best way write 'test' route's 'defaults' section?
and later, how 'module' name matched route? expect there simple way in zf2 'module', 'controller', 'action', don't know how.
option 1: produces 404 error
'defaults' => array( 'module' => 'member', 'controller' => 'test', 'action' => 'index', ), $matchedroute->getparam('module'); prints 'member' $matchedroute->getparam('controller'); prints 'test' $matchedroute->getparam('action'); prints 'index' a 404 error occurred page not found. requested controller not mapped existing controller class. controller: test(resolves invalid controller class or alias: test)
option 2: works, 'module' empty
'defaults' => array( '__namespace__' => 'member\controller', 'controller' => 'test', 'action' => 'index', ), $matchedroute->getparam('module'); prints '' <= empty $matchedroute->getparam('controller'); prints 'test' $matchedroute->getparam('action'); prints 'index' option 3: works, 'module' empty
'defaults' => array( 'controller' => 'member\controller\test', 'action' => 'index', ), $matchedroute->getparam('module'); prints '' <= empty $matchedroute->getparam('controller'); prints 'member\controller\test' $matchedroute->getparam('action'); prints 'index' i try 'module', 'controller', 'action' in onbootstrap() 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'));
you typically add route action 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 __namespace__ constant work, add following line beginning of module's config:
namespace member; extracting parameters route
on route match, router (top-level route class) returns parameters: "defaults" (parameters listed in defaults section of routing configuration) plus wildcard parameters extracted url string.
to retrieve parameter route in controller's action method, typically use params controller plugin , fromroute() method, takes 2 arguments: name of parameter retrieve , value return if parameter not present.
the fromroute() method can used retrieve parameters @ once array. that, call fromroute() without arguments, shown in example below:
// example action. public function indexaction() { // single 'id' parameter route. $id = $this->params()->fromroute('id', -1); // route parameters @ once array. $params = $this->params()->fromroute(); //... } retrieving routematch , router object
on route match, router class internally creates instance of zend\mvc\router\routematch class, providing methods extracting matched route name , parameters extracted route.
to routematch object controller's action method, can use following code:
// example action. public function indexaction() { // routematch object. $routematch = $this->getevent()->getroutematch(); // matched route's name. $routename = $routematch->getmatchedroutename(); // route parameters @ once array. $params = $routematch->getparams(); //... } for additional information zf2 routing, recommend reading using zend framework 2 book.
Comments
Post a Comment