2017-06-28 77 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

不,我不是因爲可以直接調用路由然後纔會找到 –