Zend Framework 2 中的路由 [英] Routing in Zend Framework 2
问题描述
我正在尝试在 Zend Framework 2 中进行一些路由,但它不起作用.
I'm trying to do some routing in Zend Framework 2, but it's not working.
骨架应用程序的基础工作正常,所以我在文件\module\User\config\module.config.php中添加了一个名为User的新模块和以下代码
The basics of the skeleton application are working, 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',
),
),
),
),
),
如果我尝试访问第一个路由 (/login),它会起作用.
If I try to access the first route (/login), it works.
但是第二个路由(/user/create)导致错误:
But the second route (/user/create) results in the error:
文件:
F:\www\ZendVendas\library\Zend\Mvc\Router\Http\TreeRouteStack.php:313
消息:
Route with name "create" not found
如果我创建了一个没有控制器名称的路由,它会起作用:
If I do create a route without the controller name, it works:
'create' => array(
'type' => 'Literal',
'options' => array(
'route' => '/create',
'defaults' => array(
'__NAMESPACE__' => 'User\Controller',
'controller' => 'User',
'action' => 'create',
),
),
),
但我希望路由是/user/create",而不是/create".
But I would want the route were "/user/create", and don't "/create".
我搜索了很多主题,但看不出我的错误在哪里.感谢任何帮助;)
I have searched for many topics, but can't see where is my mistake. Appreciate any help ;)
根据@Jurian 的建议调整代码
'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',
),
),
),
),
),
),
),
推荐答案
您必须了解路由在 Zend Framework 2 中的工作原理.路由有一个名称和一些配置.结构如下:
You have to understand how routing works in Zend Framework 2. Routes have a name and some configuration. The structure looks as follows:
'router' => array(
'routes' => array(
'route_name_1' => array( /* config here */ ),
'route_name_2' => array( /* config here */ ),
'route_name_3' => array( /* config here */ ),
),
),
这里的路由名称是 route_name_1
等.如果你组装一个 url,你使用 那个路由名称.因此,如果 route_name_1
具有 url /foo/bar/baz
,您可以使用 url 视图助手请求 route_name_1
的 url:
Here the route names are route_name_1
etc. If you assemble an url, you use that route name. So if route_name_1
has the url /foo/bar/baz
, you can ask for the url of route_name_1
by using the url view helper:
echo $this->url('route_name_1'); // prints /foo/bar/baz
你的 url /user/create
映射到路由 name user_create
所以要组装这个 url,你需要传递路由名称:
Your url /user/create
is mapped to the route name user_create
so to assemble this url, you need to pass on the route name:
echo $this->url('user_create'); // prints /user/create
儿童路线
还有一个子路由的概念.这可以给你一个路由 user
映射到 /user
然后这个用户路由有一个子 create
映射到 /create
因此创建的总"路径是 /user/create
.这可以配置如下:
There is also a concept of child routes. This can give you a route user
which maps to /user
and then this user route has a child create
which maps to /create
and as such the "total" route of create is /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 */ ),
),
),
),
),
现在,如果你想为 route_name_2
组合一个 url,它看起来就像上面一样:
Now, if you want to assemble an url for route_name_2
it just looks as above:
echo $this->url('route_name_1');
但是如果您需要为 child_name_1
组合 url,您可以在名称与其父级之间构建一个带有 /
的路径":
But if you need to assemble the url for child_name_1
you construct a "path" with a /
between the name and its parent(s):
echo $this->url('route_name_1/child_name_1');
因此,尽管您可以使用已有的路由名称访问 /user/create
路由,但您可能希望使用子路由,因为这为您提供了更灵活的路由系统:
So although you can access the /user/create
route fine with the route name you already have, you might want to 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',
),
),
),
),
),
),
),
然后你有一个路由 user
映射到一个配置文件".如果你组装 user/create
你去 /user/create
并且它使用来自用户控制器的createAction".user/login
路由也是如此.
Then you have a route user
which maps to a "profile". If you assemble user/create
you go to /user/create
and it uses the "createAction" from the user controller. The same hapens with user/login
route.
这篇关于Zend Framework 2 中的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!