I am trying to do some routing in Zend Framework 2, but it does not work.
The skeleton application basics work, so I added a new module called "User" and the following code in the file \ module \ User \ config \ module.config.php
'controllers' => array( 'invokables' => array( 'User\Controller\User' => 'User\Controller\UserController', ), ), 'router' => array( 'routes' => array( 'login' => array( 'type' => 'Literal', 'options' => array( 'route' => '/login', 'defaults' => array( '__NAMESPACE__' => 'User\Controller', 'controller' => 'User', 'action' => 'login', ), ), ), 'user_create' => array( 'type' => 'Literal', 'options' => array( 'route' => '/user/create', 'defaults' => array( '__NAMESPACE__' => 'User\Controller', 'controller' => 'User', 'action' => 'create', ), ), ), ), ),
If I try to access the first route (/ login), it works.
But the second route (/ user / create) leads to an error:
File:
F:\www\ZendVendas\library\Zend\Mvc\Router\Http\TreeRouteStack.php:313
Message:
Route with name "create" not found
If I create a route without a controller name, it works:
'create' => array( 'type' => 'Literal', 'options' => array( 'route' => '/create', 'defaults' => array( '__NAMESPACE__' => 'User\Controller', 'controller' => 'User', 'action' => 'create', ), ), ),
But I would like the route to be "/ user / create" and not "/ create".
I searched a lot of topics, but can't see where my mistake is. Appreciate any help;)
Edit: fixed code with @Jurian suggestions
'router' => array( 'routes' => array( 'user' => array( 'type' => 'Literal', 'options' => array( 'route' => '/user', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'profile', ), ), 'child_routes' => array( 'login' => array( 'type' => 'Literal', 'options' => array( 'route' => '/login', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'login', ), ), ), 'create' => array( 'type' => 'Literal', 'options' => array( 'route' => '/create', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'create', ), ), ), ), ), ), ),