Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework2 ZF2-Zend Framework 2,了解路由_Zend Framework2 - Fatal编程技术网

Zend framework2 ZF2-Zend Framework 2,了解路由

Zend framework2 ZF2-Zend Framework 2,了解路由,zend-framework2,Zend Framework2,我正试图了解ZF2中的模块布线 目前,我只能为一个动作创建一个控制器,我正在努力找出这个路由。我已经看过了其他模块和插件,我有点明白了,只需要轻轻一推就能“明白” 在这个例子中,我试图路由到两个动作:indexAction和cmsToolAction 基本上,用户导航到: /affiliates/overview /affiliates/cmstools 错误是: The requested URL could not be matched by routing. 我想我首先要做的是理解MV

我正试图了解ZF2中的模块布线

目前,我只能为一个动作创建一个控制器,我正在努力找出这个路由。我已经看过了其他模块和插件,我有点明白了,只需要轻轻一推就能“明白”

在这个例子中,我试图路由到两个动作:indexAction和cmsToolAction

基本上,用户导航到:

/affiliates/overview
/affiliates/cmstools
错误是:

The requested URL could not be matched by routing.
我想我首先要做的是理解MVC是如何工作的,然后迷失在细节中。手册中的信息太多了,让人有点不知所措

无论如何-非常感谢您的任何意见

模块结构图:

我的控制器如下所示:

<?php
namespace Affiliates\Controller;
use Zend\Mvc\Controller\AbstractActionController;

class AffiliatesController extends AbstractActionController
{
    //Overview page
    public function IndexAction()
    {

    }

    public function CmstoolsAction()
    {

    }


}
<?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
的路由,它有一个子路由用于
/cmtool
。这将与以下URL匹配:

/overview
/overview/cmstool
不完全是你想要的

有几种不同的方法可以配置此功能。与您当前拥有的最接近的是
/subfiliates
的路由,有两个子路由,每个子路由用于您的操作。这方面的配置是:

'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',
                        ),
                    ),
                ),
            ),
        ),
    ),
),
此配置包含三条路由:
附属公司
概述
cmstools
。后两个都是附属公司的子路线。注意,我从附属机构路由中删除了行
“may\u terminate”=>true,
。这决定附属机构路由是否会自行匹配(即URL
/affiliates
是否会工作)。既然你没有列出这个,我想你不会想要的

另一种配置方法是简单地创建两个文本路由,每个URL一次(根本不使用子路由):


我将“controller”=>“Affiliates”更新为::“controller”=>“Affiliates/controller/affiliatecontroller”,它像炸弹一样工作!谢谢你的意见!啊,是的,错过了。“控制器”键需要与您为模块配置中的控制器第10行设置的别名匹配。因此,与其说是
附属公司
,不如说是
附属公司\控制器\附属公司
再问一个问题。如果我创建文字路由并选择不将其作为模块的子路由(在本例中为附属路由),根据我的理解,路由命名将变得非常重要,以免它覆盖来自其他模块的路由。在完成这项工作后,我去更新了另一个模块,花了几个小时试图找出它为什么不起作用。当我将命名约定从:overview更改为overview 2时,路由再次开始工作。我想这就是为什么在应用程序中有子路由很重要的原因…它们是否是子路由与此无关。您的路由必须具有唯一的名称,否则它们将相互覆盖。很高兴知道,我们将通过重新编码子路由来学习这一课:SNote文件夹树中的一个小错误。我将视图目录更新为:view/affiliates/affiliates/
'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',
                ),
            ),
        ),
    ),
),