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

routing after authentication doesn't work properly

我可能对路由的工作方式有理解上的问题。我尝试了一些 zend-authentication,你可以在下面看到控制器代码。 如果身份验证有效,我想要路由到另一个控制器索引操作。应该很简单,但是路由不起作用我留在登录表单。我添加了一个回声只是为了查看身份验证后我在哪里并且身份验证有效。这是代码:

$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()];
        }
    }

如您所见,我尝试了几种可能性。另一个控制器放在同一个模块中。

此处附加目标控制器的索引操作。

我还没有说完,因为错误的路由我切换了其他所有内容,所以当我到达那里时它只显示回显文本。

 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(),
//                  ]);
//      }

    }

编辑 1:添加 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',
                                    ],
                            ],

                    ],

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

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

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

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

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