身份验证后路由无法正常工作

时间:2017-06-28 07:25:09

标签: zend-framework zend-framework3 zend-auth

我可能对路由的工作原理有所了解。我尝试了一下zend-authentication,你可以看到下面的控制器代码。 如果身份验证是valide我想要路由到另一个控制器索引操作。应该很简单,但路由不起作用我留在loginform。我添加了一个回声,只是为了看看我在认证后的位置,并且认证是valide。这是代码:

$form = new LoginForm();
    //return ['form' => $form];
    $form->get('submit')->setValue('Login');    
    //echo "hier";
    //$this->layout()->setTemplate('layout/login-layout');
    $request = $this->getRequest();

    if (! $request->isPost()) {     
        return ['form' => $form];   
    }
    else {
        $username=$request->getPost('accessname');
        $password=$request->getPost('passwort');
        //echo $password;
        $adapter = $this->authService->getAdapter();
        $adapter->setIdentity($request->getPost('accessname'));
        $adapter->setCredential($request->getPost('passwort'));

        $result = $this->authService->authenticate();

        if ($result->isValid()){
            echo "valide";
            //return $this->redirect()->toRoute('import');
            return $this->redirect()->toRoute('import', ['action' => 'index']);
        }
        else{
            return ['form' => $form, 'messages' => $result->getMessages()];
        }
    }

你可以看到我尝试了几种可能性。另一个控制器放在同一个模块中。

此处还有destinationcontroller的索引操作。

我没有在那里完成,因为错误的路由我切换了其他所有东西,所以当我降落时它只显示了回音文本。

 public function indexAction()
    {
        echo "importcontroller";
    //  $result = $this->authService->has
    //  $auth=Zend_Auth::getInstance();
//      $adapter = $this->authService->getAdapter();

//      if(!$this->authService->hasIdentity())
//      {
//          return $this->redirect()->toRoute('./index/index');
//      }
//      else {
//          return new ViewModel([
//              'projects' => $this->projectTable->fetchAll(),
//                  ]);
//      }

    }

EDIT1:添加module.confg.php

'router' => [
            'routes' => [
                    'import' => [
                            'type'    => Segment::class,
                            'options' => [
                                    'route' => '/import[/:action[/:id]]',
                                    'constraints' => [
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ],
                                    'defaults' => [
                                            'controller' => Controller\ImportController::class,
                                            'action'     => 'index',
                                    ],
                            ],

                    ],
                    'project' => [
                            'type'    => Segment::class,
                            'options' => [
                                    'route' => '/project[/:action[/:id]]',
                                    'constraints' => [
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ],
                                    'defaults' => [
                                            'controller' => Controller\ProjectController::class,
                                            'action'     => 'index',
                                    ],
                            ],

                    ],
                    'unit' => [
                            'type'    => Segment::class,
                            'options' => [
                                    'route' => '/unit[/:action[/:id]]',
                                    'constraints' => [
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ],
                                    'defaults' => [
                                            'controller' => Controller\UnitController::class,
                                            'action'     => 'index',
                                    ],
                            ],

                    ],

                    'index' => [
                            'type'    => Segment::class,
                            'options' => [
                                    'route' => '/index[/:action[/:id]]',
                                    'constraints' => [
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ],
                                    'defaults' => [
                                            'controller' => Controller\IndexController::class,
                                            'action'     => 'index',
                                    ],
                            ],

                    ],
                    'user' => [
                            'type'    => Segment::class,
                            'options' => [
                                    'route' => '/user[/:action[/:id]]',
                                    'constraints' => [
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ],
                                    'defaults' => [
                                            'controller' => Controller\UserController::class,
                                            'action'     => 'index',
                                    ],
                            ],

                    ],
                    'followup' => [
                            'type'    => Segment::class,
                            'options' => [
                                    'route' => '/followup[/:action[/:id]]',
                                    'constraints' => [
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ],
                                    'defaults' => [
                                            'controller' => Controller\FollowupController::class,
                                            'action'     => 'index',
                                    ],
                            ],

                    ],

1 个答案:

答案 0 :(得分:1)

你能用路由配置显示module.config.php的内容吗?

也许您的路由错误并在正确身份验证之前重定向到您所在的同一页面?

P.S。我认为,您应始终从表单中获取数据,并通过$form->getDate()函数获取。当然,在您应该应用适当的过滤器之前。

这个概念在zend框架教程中描述:

https://docs.zendframework.com/tutorials/getting-started/forms-and-actions/