Search code examples
zend-frameworkzend-framework3zend-auth

routing after authentication doesn't work properly


I probably have an understanding issue how the routing works. I tried a bit with zend-authentication, you can see the controllercode below. If the authentication is valide I want a routing to another controller index action. Should be quite simple, but the routing doesn't work I stay at the loginform. I added an echo just to see where I am after authentication and the authentication is valide. Here is the code:

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

as you can see I tried several possibilities. The other controller is placed in the same module.

Here additional the index action of the destinationcontroller.

I'm not finished there, because of the false routing I toggled everything else, so it just shows the echotext when I land there.

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

                    ],

Solution

  • Can you show contents of module.config.php with routing configuration?

    Maybe your routing was wrong and redirect to the same page where you are before correct authentication?

    P.s. I think, You should always filternig data from form and get it by $form->getDate() function. Of course before you should apply appropriate filters.

    this concept is described in zend framework tutorial:

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