Routing in Zend Framework 2

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', ), ), ), ), ), ), ), 
+6
source share
2 answers

You need to understand how routing works in Zend Framework 2. Routes have a name and some configuration. The structure is as follows:

 'router' => array( 'routes' => array( 'route_name_1' => array( /* config here */ ), 'route_name_2' => array( /* config here */ ), 'route_name_3' => array( /* config here */ ), ), ), 

Here are the route names route_name_1 , etc. If you are collecting the URL, you are using that route name. Therefore, if route_name_1 has url /foo/bar/baz , you can request the route_name_1 URL using the URL lookup helper:

 echo $this->url('route_name_1'); // prints /foo/bar/baz 

Your url /user/create maps to the user_create route user_create , so to build this URL you need to pass the route name:

 echo $this->url('user_create'); // prints /user/create 

KIDS ROUTES

There is also the concept of children's routes. This can give you a user route that maps to /user , and then this user route has a create child element that maps to /create , and thus the "common" creation route /user/create . This can be configured as follows:

 'router' => array( 'routes' => array( 'route_name_1' => array( /* config here */ ), 'route_name_2' => array( /* config here */ 'child_routes' => array( 'child_name_1' => array( /* config here */ ), 'child_name_2' => array( /* config here */ ), ), ), ), ), 

Now, if you want to collect the URL for route_name_2 , it just looks like this:

 echo $this->url('route_name_1'); 

But if you need to collect the url for child_name_1 , you will create a "path" with / between the name and its parent (s):

 echo $this->url('route_name_1/child_name_1'); 

So, although you can access the /user/create route with the name of the route that you already have, you can use child routes as this gives you a more flexible routing system:

 'router' => array( 'routes' => array( 'user' => array( 'type' => 'Literal', 'options' => array( 'route' => '/user/create', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'profile', ), ), ), 'child_routes' => array( 'login' => array( 'type' => 'Literal', 'options' => array( 'route' => '/login', 'defaults' => array( 'action' => 'login', ), ), ), 'create' => array( 'type' => 'Literal', 'options' => array( 'route' => '/create', 'defaults' => array( 'action' => 'create', ), ), ), ), ), ), ), 

Then you have a user route that maps to a "profile". If you compile user/create , you will go to /user/create and it uses "createAction" from the user controller. The same thing happens with the user/login route.

+14
source

I found what I was doing wrong.

One of my view files has a URL function pointing to the /create route. It would be very helpful if Zend indicated the file with the wrong route, but as soon as I found the error, everything works now.

 'router' => array( 'routes' => array( 'login' => array( 'type' => 'Literal', 'options' => array( 'route' => '/login', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'login', ), ), ), 'logout' => array( 'type' => 'Literal', 'options' => array( 'route' => '/logout', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'logout', ), ), ), 'user' => array( 'type' => 'Literal', 'options' => array( 'route' => '/user', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'profile', ), ), 'child_routes' => array( 'create' => array( 'type' => 'Literal', 'options' => array( 'route' => '/create', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'create', ), ), ), 'edit' => array( 'type' => 'Literal', 'options' => array( 'route' => '/edit', 'defaults' => array( 'controller' => 'User\Controller\User', 'action' => 'edit', ), ), ), ), ), ), ), 

Thanks for the help!

0
source

Source: https://habr.com/ru/post/955162/


All Articles