Zend Framework 2 默认模块 [英] Zend Framework 2 default module

查看:37
本文介绍了Zend Framework 2 默认模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In ZF1 you did not have to include the module in the URL; if it was not provided, it would default to... the default module. How can this be accomplished in ZF2? I have used the skeleton application to get up and running, but it seems as if I always need to include the module name, e.g. /application/controller/action.

我想我可以通过创建一个带有两个占位符"的路由来解决这个问题;控制器和动作,然后将默认模块设置为应用程序".然后我会将它放在 /config/autoload/global.php(或者可能是 /config/application.config.php)中,以便该路由适用于我的所有应用程序.但是,我收到错误消息,指出 URL 无法通过路由匹配,即使我将路由硬编码为 /user/index 之类的内容.

I figured I could work around this by creating a route with two "placeholders"; controller and action, and then set the default module to "application". I would then put this in /config/autoload/global.php (or perhaps /config/application.config.php) so that the route applies for all of my application. However, I am getting the error message that the URL could not be matched by routing, even if I hard code the route to something like /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
                    )
                )
            ),
        )
    )
);

正如我在评论中写的那样,我不确定我的问题是否与默认语法有关,但我不这么认为,因为如果我对路由进行硬编码并删除所有默认值,也会发生同样的情况.我还尝试基于骨架应用程序中的示例对其进行试验,但没有运气.我是不是用错了方法?有没有更好的方法?还是我只是弄错了?

As I wrote as a comment, I am not sure if my problem is with the defaults syntax, but I wouldn't think so as the same happens if I hard code the route and remove all defaults. I also tried to experiment with it based on examples in the skeleton application, but without luck. Am I going about it the wrong way? Is there a better approach? Or did I just make a mistake?

提前致谢.

有关使其工作的代码,请参阅答案.有关如何工作的说明,请阅读这篇文章.

For the code to make it work, see the answer. For an explanation of how it works, read this article.

推荐答案

注意:强烈建议使用显式路由而不是通配符.

Note: Explicit routes are strongly recommended over wildcard.

您在尝试中使用了 Zend\Mvc\Router\Http\Literal 路由类型,因为您可能猜到它是字面量,即精确匹配.要使其工作,您需要分段路由类型.

You used Zend\Mvc\Router\Http\Literal route type in your attempt, as you might guess it is literal, ie exact match. To make it work you need segment route type.

检查application 路由 和它的子路由 default.它完全符合您的要求.

Check application route in Zend Skeleton Application config and it's child route default. It does exactly what you are trying to do.

至于模块 - 从您的代码角度来看,没有模块"这样的东西.模块在启动时注册资源,此后不再相关.在 zf2 中,您可以通过使用 controllerManager 注册的控制器的类或别名指定确切的控制器

As for modules - there is no such thing as 'module' from your code perspective. Module registers resources on startup and it is no longer relevant after that point. In zf2 you specify exact controller by class or alias name under which controller registered with 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'
        ),
    )
);

这篇关于Zend Framework 2 默认模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆