我想在 Zend Framework 2 中创建一个通用模块/ Controller /操作路由,以与 ZF2 MVC 架构一起使用。
在 ZF1 中,默认路由定义为 /[:module][/:controller][/:action]
,其中模块默认为 default
, Controller 默认index
和 index
的操作。
现在,ZF2 改变了模块的预期方式,从简单的 Controller 和 View 组到真正的独立应用程序,将 Controller 名称显式映射到 Controller 类。
由于所有 Controller 名称在所有模块中都必须是唯一的,我想将它们命名为 modulename-controllername
但我希望 URL 看起来像 /modulename/controllername
无需为每个模块创建特定路由,使用类似于上述 ZF1 的旧默认路由。
最佳答案
是的,这是很有可能的,但是您需要做一些工作。使用以下配置:
'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 对象。这并不难,看看框架中已有的其他路由匹配器,您就会明白这一点。
关于php - 如何在 Zend Framework 2 中创建通用模块/ Controller /操作路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938809/