2017-06-28 49 views
0

我可能有一个理解问题如何路由工作。我尝试了一下zend-authentication,你可以看到下面的controllercode。 如果身份验证是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()]; 
     } 
    } 

正如你所看到的,我尝试了几种可能性。另一个控制器放置在同一个模块中。

此处额外添加了目标控制器的索引操作。

我还没有完成,因为错误的路由我切换了其他所有内容,所以它只是显示了我登陆那里的回声文。

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

你能显示出与路由配置的module.config.php内容是什么?

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

P.s.我认为,你应该总是从表单中获取filternig数据,并通过$form->getDate()函数得到它。当然,你应该适用适当的过滤器。

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

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

+0

我编辑我的职务。我不明白你的ps,你能解释给我吗? –

+0

你的'module.config.php'看起来不错,也许你在'ImportController.php'的'Module.php'或'onDispatch'函数中重定向? – Valvadis

+0

不,我不是因为可以直接调用路由然后才会找到 –