与ZF2登录连接时出错

时间:2016-09-01 14:18:15

标签: php authentication zend-framework doctrine-orm

目前我正在创建一个身份验证模块。 我使用Zend Framework 2.5.1,与Doctrine 2结合使用。

当我尝试连接时,一旦发布我的表单,我就会出现以下错误:缺少参数" id"

以下是我的登录操作(AuthController中的 ):

public function loginAction()
{
    /** @var ServiceLocator $serviceLocator */
    $serviceLocator = $this->getServiceLocator();

    /** @var Authenticate $authentication */
    $authentication = $serviceLocator->get('service.user.auth');

    if ($authentication->hasIdentity()) {
        $this->redirect()->toRoute('home/user',
        [
            'controller' => 'user',
            'action'     => 'profile',
        ], [], true);
    }

    if (($prg = $this->postRedirectGet()) instanceof Response) {
        return $prg;
    }

    /** @var Login $form */
    $form = $authentication->getForm();

    if ($prg !== false) {

        $form->setData($prg);

        if ($authentication->authenticate()) {

            $this->redirect()->toRoute('home/user',
            [
                'controller' => 'user',
                'action'     => 'profile',
            ], [], true);
        }

        if ($form->isValid()) {
            $messages = $authentication->getMessages();
        }
    }

    return compact('form', 'messages');
}

与我的身份验证匹配的路由:

return
[
    'router' =>
    [
        'routes' =>
        [
            'home' =>
            [
                'child_routes' =>
                [
                    'user' =>
                    [
                        'type' => 'Segment',
                        'options' =>
                        [
                            'route' => 'user[/:action][/:id]',
                            'defaults' =>
                            [
                                'controller' => 'user/user',
                                'action'     => 'index',
                            ],
                            'constraints' =>
                            [
                                'action' => 'index|register|update|profile',
                                'id'     => '[0-9]+'
                            ],
                        ],
                        'may_terminate' => true,
                        'child_routes' =>
                        [
                            'auth-login' =>
                            [
                                'type' => 'Literal',
                                'options' =>
                                [
                                    'route' => '/login',
                                    'defaults' =>
                                    [
                                        'controller' => 'user/auth',
                                        'action' => 'login'
                                    ],
                                    'constraints' =>
                                    [
                                        'action' => 'login|logout',
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];

欢迎提供一些帮助,我不确定为什么会出现这种错误..

非常感谢!

2 个答案:

答案 0 :(得分:0)

在配置中,请'id' => '[0-9]*'使用*代替+,因为+表示它应至少有一位数。

另外,您是否查看了https://github.com/ZF-Commons/ZfcUser/,它提供了您可以使用的功能。除了许多RBAC和访问列表授权模块之外,您还可以将它与Doctrine一起使用...

答案 1 :(得分:0)

我尝试你说的一切,这是同样的错误。在ZF2中调试路由并不容易。

有关信息,我有2个模块:应用程序和用户。 所以它是应用程序模块的路由:

<?php
return
[
    'router' =>
    [
        'routes' =>
        [
            'home' =>
            [
                'type'    => 'Literal',
                'options' =>
                [
                    'route'    => '/',
                    'defaults' =>
                    [
                        'controller' => 'application.index',
                        'action'     => 'index',
                    ],
                ],
                'may_terminate' => true,
            ],
        ],
    ]
];