如何在Zend Framework 2中创建通用模块/控制器/动作路线? [英] How to create a generic module/controller/action route in Zend Framework 2?

查看:82
本文介绍了如何在Zend Framework 2中创建通用模块/控制器/动作路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Zend Framework 2中创建一个通用模块/控制器/动作路由,以用于ZF2 MVC架构.

I would like to create a generic module/controller/action route in Zend Framework 2 to be used with ZF2 MVC architecture.

在ZF1中,默认路由的定义类似于/[:module][/:controller][/:action],其中模块默认为default,控制器默认为index,动作为index.

In ZF1 the default route was defined like /[:module][/:controller][/:action] where module would default to default, controller would default to index and action to index.

现在,ZF2通过将控制器名称显式映射到控制器类,从简单的控制器和视图组到真正的独立应用程序,改变了模块的设计意图.

Now, ZF2 changed the way modules are intended, from simple groups of controllers and views, to real standalone applications, with explicit mapping of controller name to controller class.

由于所有控制器名称在所有模块中都必须是唯一的,因此我想将其命名为modulename-controllername,但我希望URL看起来像/modulename/controllername,而无需为每个模块创建特定的路由,例如使用上述ZF1的旧默认路由.

Since all controller names must be unique across all modules, I was thinking to name them like modulename-controllername but I would like the URL to look like /modulename/controllername without the need to create specific routes for each module, using something like the old default route for ZF1 described above.

推荐答案

是的,这是很有可能的,但是您需要做一些工作.使用以下配置:

Yes it is very possible, but you will have to do a little work. Use the following config:

        'default' => array(
            'type'    => 'My\Route\Matcher',
            'options' => array(
                'route'    => '/[:module][/:controller[/:action]]',
                'constraints' => array(
                    'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'module'     => 'default',
                    'controller' => 'index',
                    'action'     => 'index',
                ),
            ),
        ),

然后,您必须编写自己的My\Route\Matcher来创建MVC可以使用的Routemap对象.不难,看看框架中已有的其他路由匹配器,您就会明白.

Then you have to write your own My\Route\Matcher to create a Routemap object that the MVC can use. It's not hard, look at the other route matchers already in the framework and you'll get the idea.

这篇关于如何在Zend Framework 2中创建通用模块/控制器/动作路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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