Php Zend Framework 2默认模块

Php Zend Framework 2默认模块,php,zend-framework2,Php,Zend Framework2,在ZF1中,您不必在URL中包含模块;如果没有提供,它将默认为。。。默认模块。如何在ZF2中实现这一点?我已经使用了框架应用程序来启动和运行,但似乎我总是需要包含模块名称,例如/application/controller/action 我想我可以通过创建带有两个“占位符”的路线来解决这个问题;控制器和操作,然后将默认模块设置为“应用程序”。然后我会将其放入/config/autoload/global.php(或者/config/application.config.php)中,以便路由适用于

在ZF1中,您不必在URL中包含模块;如果没有提供,它将默认为。。。默认模块。如何在ZF2中实现这一点?我已经使用了框架应用程序来启动和运行,但似乎我总是需要包含模块名称,例如
/application/controller/action

我想我可以通过创建带有两个“占位符”的路线来解决这个问题;控制器和操作,然后将默认模块设置为“应用程序”。然后我会将其放入
/config/autoload/global.php
(或者
/config/application.config.php
)中,以便路由适用于我的所有应用程序。但是,我收到一条错误消息,URL无法通过路由进行匹配,即使我将路由硬编码为
/user/index

我尝试了下面的代码

return array(
    'router' => array(
        'routes' => array(
            'nomodule' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/:controller/:action',
                    'constraints' => array(
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                    ),
                    'defaults' => array(
                        'module' => 'Application' // Not sure of the syntax here
                    )
                )
            ),
        )
    )
);
正如我在评论中所写的,我不确定我的问题是否与默认语法有关,但如果我硬编码路由并删除所有默认值,我不会这样认为。我还尝试根据骨架应用程序中的示例进行实验,但运气不佳。我走错方向了吗?有更好的方法吗?还是我只是犯了个错误

提前谢谢


编辑:有关使其工作的代码,请参阅答案。有关其工作原理的解释,请阅读。

注意:强烈建议显式路由优于通配符

您在尝试中使用了Zend\Mvc\Router\Http\Literal路由类型,因为您可能会猜测它是Literal,即完全匹配。要使其工作,您需要管段管线类型

它是子路由
默认值
。 它正是你想要做的

至于模块——从代码的角度来看,没有“模块”这样的东西。模块在启动时注册资源,并且在该点之后不再相关。 在zf2中,您可以按类或别名指定控制器的确切名称,控制器在该名称下向controllerManager注册

// module\Application\config\module.config.php
return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                            ),
                            'defaults' => array(
                                'action' => 'index',
                                '__NAMESPACE__' => 'Application\Controller'
                            )
                        )
                    )
                )
            )
        )
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\User' => 'Application\Controller\UserController'
        ),
    )
);

我对采埃孚基本上是新手,我刚刚开始学习,所以尝试了这个,它对我很有效。只是为了确保,当你转到你的域URL并且不键入控制器名称时,你想更改你的默认模块,对吗

转到module.config.php

“路由器”=>阵列(
“路由”=>数组(
“URL中带有控制器的相册”=>数组(
'类型'=>'段',
“选项”=>数组(
“路由”=>“/相册[/:操作]”,
'约束'=>数组(
“行动”=>“[a-zA-Z][a-zA-Z0-9_u-]*”,
),
“默认值”=>数组(
'controller'=>'Album\controller\Album',
“操作”=>“索引”,
),
),
),
“URL中没有控制器的相册”=>数组(
'类型'=>'段',
“选项”=>数组(
“路由”=>“/[:操作]”,
'约束'=>数组(
“行动”=>“[a-zA-Z][a-zA-Z0-9_u-]*”,
),
“默认值”=>数组(
'controller'=>'Album\controller\Album',
“操作”=>“索引”,
),
),
),
'将其设置为您的主页'=>数组(
'类型'=>'段',
“选项”=>数组(
“路由”=>“/”,
“默认值”=>数组(
'controller'=>'Album\controller\Album',
“操作”=>“索引”,
),
),
),
),
),

正如我在@Xerkus answer下的评论中所述,它并不适用于所有URL:

/application/index/index
/application/index
/application
/index/index
/index
/
我还向
IndexController
TestController
添加了
testAction()
,操作与
IndexController
相同,因此我还可以在以下路径上测试我的解决方案:

/index/test
/test/index
/test/test
/test
所以经过一些研究(主要是研究),我为他们准备了解决方案。我将粘贴整个
module.config.php
数组:

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            'noModule' => array(
                'type' => 'Segment',
                'options' => array(
                    'route'    => '/[:controller[/:action]]',
                    'constraints' => array(
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
            ),
            // The following is a route to simplify getting started creating
            // new controllers and actions without needing to create a new
            // module. Simply drop new controllers in, and you can access them
            // using the path /application/:controller/:action
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\Test' => 'Application\Controller\TestController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);
与Zend 2 Skeleton应用程序配置相比,我添加了
noModule
路由和新的
controller invokable
-
test
。当然,
noModule
route包含
Application/Controller
名称空间,因此基于这一事实,您可以设置您需要的任何默认模块。现在它正常工作了


当然要记住,您的
noModule
路由应该在
application.config.php
的第一个模块中定义,以确保它始终优先。还请记住,应谨慎使用默认模块解决方案,以避免控制器和模块名称之间的冲突,例如,如果您为下一个模块命名
Index
,显然,您将与
应用程序
模块中的
IndexController
发生命名冲突。

您的约束必须匹配路由中的占位符(例如/:action)才能产生任何效果。我将添加一个提示,即每个控制器都必须列在可调用项中,要将它们从url中解析出来,invokables中使用的符号必须与url中使用的符号相匹配。@SamuelHerzog它不需要在
invokables
中,也可以配置为factory。当然,因为您使用了invokables,我认为指出这一点比添加其他信息更有意义,这就是您的任务:)谢谢您的回答。为了让代码正常工作,我不得不做一些小的调整,但你为我指明了正确的方向。我已编辑了您答案中的代码以反映我的编辑。我从segments路由中删除了斜杠(因为它是
/
路由的子路由),并将
\uuuu名称空间\uuuu
设置到我的控制器目录。通过这样做,我可以在URL中写入
localhost/user
,并将其映射到
Application\Controller\user
,而无需