ZF2从控制器/索引/登录中删除索引

时间:2014-05-13 10:25:38

标签: zend-framework2

我正在尝试从url中删除索引操作。刚刚用auth模块启动了ZF2。 我的module.config.php如下所示。

使用以下代码可以看到登录屏幕,可以登录和注销,但是当涉及到注册和密码重置选项时,网址/ auth / index / registration而不是domain / auth / registration。

非常感谢任何帮助。 提前致谢。

return array(

'controllers' => array(
    'invokables' => array(
        'Auth\Controller\Index' => 'Auth\Controller\IndexController',
        'Auth\Controller\Registration' => 'Auth\Controller\RegistrationController',
        'Auth\Controller\Admin' => 'Auth\Controller\AdminController',
    ) ,
) ,
'router' => array(
    'routes' => array(
        'auth' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/auth[/:action]',
                'defaults' => array(
                    '__NAMESPACE__' => 'Auth\Controller',
                    'controller' => 'Index',
                    'action' => 'index',
                ) ,
            ) ,
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/[:controller[/:action[/:id]]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'id' => '[a-zA-Z0-9_-]*',
                        ) ,
                        'defaults' => array() ,
                    ) ,
                ) ,
            ) ,
        ) ,
    ) ,
) ,
'view_manager' => array(

    //        'template_map' => array(
    //            'layout/Auth'           => __DIR__ . '/../view/layout/Auth.phtml',
    //        ),

    'template_path_stack' => array(
        'auth' => __DIR__ . '/../view'
    ) ,
    'display_exceptions' => true,
) ,
'service_manager' => array(

    // added for Authentication and Authorization. Without this each time we have to create a new instance.
    // This code should be moved to a module to allow Doctrine to overwrite it

    'aliases' => array( // !!! aliases not alias
        'Zend\Authentication\AuthenticationService' => 'my_auth_service',
    ) ,
    'invokables' => array(
        'my_auth_service' => 'Zend\Authentication\AuthenticationService',
    ) ,
) ,

);

1 个答案:

答案 0 :(得分:0)

您可以尝试使用“literal”而不是“segment”类型来为此路由从网址中删除索引:

'auth' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route'    => '/auth/registration[:/]',
         'defaults' => array(
             '__NAMESPACE__' => 'Auth\Controller',
             'controller'    => 'Index',
             'action'        => 'index',
         ),
     ),
),

来自zf2文档:“Literal路由用于完全匹配URI路径。因此,配置只是您要匹配的路径,以及您希望在匹配时返回的”defaults“或参数。” http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html#zend-mvc-router-http-literal