如何在Zend Framework 2中创建通用模块/控制器/动作路由?

时间:2012-06-07 19:46:57

标签: php zend-framework2 zend-framework-modules

我想在Zend Framework 2中创建一个与ZF2 MVC架构一起使用的通用模块/控制器/动作路由。

在ZF1中,默认路由定义为/[:module][/:controller][/:action],其中模块默认为default,控制器默认为index,操作为index

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

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

3 个答案:

答案 0 :(得分:8)

是的,这很有可能,但你必须做一些工作。使用以下配置:

        '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对象。这并不难,看看框架中已有的其他路线,你就会明白这一点。

答案 1 :(得分:1)

如果使用Zend Skeleton Application,则表示您已配置此默认控制器。

请参阅此处https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php

答案 2 :(得分:0)

要为zf2模块建立通用/标准路由系统,这是我对一个控制器“module \ controller \ index”(默认控制器)的解决方案:

'router' => array(
    'routes' => array(              
        'default' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/', // <======== this is take the first step to our module "profil"
                'defaults' => array(
                    'module'     => 'profil',
                    'controller' => 'profil\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),              
        'profil' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/[profil][/:action]', // <======== this is take the next steps of the module "profil"
                'constraints' => array(
                    'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array( // force the default one
                    'module'     => 'profil',
                    'controller' => 'profil\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

然后在我们的控制器“profil \ Controller \ Index”中我们有三个动作“index”“home”“signout”:

public function indexAction()
{
        if ($this->identity()) {
            return $this->redirect()->toRoute('profil',array('action'=>'home'));
        } else {
            // ......
                    $authResult = $authService->authenticate();
                    if ($authResult->isValid()) {
                            //......
                                                    return $this->redirect()->toRoute('profil',array('action'=>'home'));
                    } else {
                        // ......
                    }
                } else {
                    $messages = $form->getMessages();
                }
            }               
            return new ViewModel();
        }
}

public function homeAction()
{
    if (!$this->identity()) {
        return $this->redirect()->toRoute('profil',array('action'=>'signout'));
    }
}

public function signoutAction()
{
    if ($this->identity()) {
        $authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
        $authService->clearIdentity();
    }
    $this->redirect()->toRoute('profil');
}  
无论如何,谢谢你。)