【问题标题】:routing after authentication doesn't work properly身份验证后的路由无法正常工作
【发布时间】:2017-06-28 07:25:08
【问题描述】:

我可能对路由的工作原理有一个理解问题。我尝试了一些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()];
        }
    }

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

这里增加了destinationcontroller的index动作。

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

 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',
                                    ],
                            ],

                    ],

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 我编辑了我的帖子。没看懂你的ps,能解释一下吗?
    • 你的module.config.php 看起来不错,也许你在Module.phponDispatch 函数中有重定向ImportController.php
    • 不,我没有,因为a可以直接调用路由,然后就会被找到
    • 你可以尝试在控制器的开头添加重定向吗?有效吗?
    • 你确定$result->isValid()返回非假值吗?
    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 2014-11-17
    • 2012-10-20
    • 1970-01-01
    • 2020-05-18
    • 2015-02-16
    • 1970-01-01
    • 2018-02-13
    相关资源
    最近更新 更多