ZF2 - Zend Framework 2,理解路由 [英] ZF2 - Zend Framework 2, understanding routing
问题描述
我正在尝试了解 ZF2 中的模块路由.
I am trying to get my head around Module routing in ZF2.
目前我只能为单个动作创建一个控制器,并且正在努力弄清楚这个路由.我看过其他模块和插件,我有点明白了,只需要一点点推动就可以得到它".
At the moment I am only able to create a single controller for a single action and am struggling to figure this routing out. I have looked at other modules and plugins and I kind of get it, just need a small push to "get it".
在这个例子中,我试图路由到两个动作:indexAction 和 cmstoolsAction
In this example I am trying to route to the two actions: indexAction and cmstoolsAction
基本上用户导航到:
/affiliates/overview
/affiliates/cmstools
错误是:
The requested URL could not be matched by routing.
我认为我在挣扎的地方首先是理解 MVC 的工作原理并迷失在细节中.手册中的信息太多了,有点让人不知所措.
I think where I am struggling is firstly comprehending how MVC works and getting lost in the detail. There is so much information in the manual that it becomes a little overwhelming.
无论如何 - 非常感谢任何输入!
anyway - would greatly appreciate any input!
模块结构图:
我的控制器如下所示:
<?php
namespace Affiliates\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class AffiliatesController extends AbstractActionController
{
//Overview page
public function IndexAction()
{
}
public function CmstoolsAction()
{
}
}
我的模块配置如下所示:
And my module config looks like this:
<?php
return array(
'view_manager' => array(
'template_path_stack' => array(
'affiliates' => __DIR__ . '/../view',
),
),
'controllers' => array(
'invokables' => array(
'Affiliates\Controller\Affiliates' =>
'Affiliates\Controller\AffiliatesController'
),
),
'router' => array(
'routes' => array(
'affiliates' => array(
'type' => 'Literal',
'options' => array(
'route' => '/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
),
),
);
推荐答案
路由配置是这里唯一重要的部分.目前,您有一个 /overview
的路由,它有一个 /cmstool
的子路由.这将匹配以下网址:
The routing config is the only important part here. At the moment, you have a route for /overview
, which has a child route for /cmstool
. This would match the following URLs:
/overview
/overview/cmstool
不完全是你想要的.
您可以通过几种不同的方式进行配置.最接近您当前拥有的路线是 /affiliates
的路线,有两个子路线,每个动作一个.此配置将是:
There are a few different ways you could configure this. The one closest to what you currently have would be a route for /affiliates
, with two child routes, one for each of your actions. The configuration for this would be:
'router' => array(
'routes' => array(
'affiliates' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
'child_routes' => array(
'overview' => array(
'type' => 'Literal',
'options' => array(
'route' => '/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
),
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
),
),
此配置包含三个路由:affiliates
、overview
和 cmstools
.后两者都是附属公司的子路由.请注意,我删除了行 'may_terminate' =>真的,
来自附属公司的路线.这决定了affiliates 路由是否自己匹配(即URL /affiliates
是否有效).由于你没有列出这个,我假设你不希望它.
This configuration contains three routes: affiliates
, overview
and cmstools
. The latter two are both child routes of affiliates. Note that I removed the line 'may_terminate' => true,
from the affiliates route. This determines whether or not the affiliates route would match on its own (i.e. whether the URL /affiliates
would work). Since you didn't list this I'm assuming you don't want it to.
另一种配置方法是简单地创建两个文字路由,每个 URL 一次(根本不使用子路由):
Another way you could configure this would be to simply create two literal routes, once for each URL (not using child routes at all):
'router' => array(
'routes' => array(
'overview' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates/overview',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'index',
),
),
),
'cmstools' => array(
'type' => 'Literal',
'options' => array(
'route' => '/affiliates/cmstools',
'defaults' => array(
'controller' => 'Affiliates',
'action' => 'cmstools',
),
),
),
),
),
这篇关于ZF2 - Zend Framework 2,理解路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!