2
votes

I am beginner working with Zend Framework 2.3.3. I'm trying to do some routing,but it's always not working. I added a new module called Manager, the following code in the file \module\Manager\config\module.config.php

'router' => array(
    'routes' => array(
        'auth' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/tologinpage',
                'defaults' => array(
                    'controller' => 'Manager\Controller\Auth',
                    'action'     => 'show-login',
                ),
            ),
            'may_terminate' => true, 
            'child_routes' => array(
                'login'  => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route'    => '/login',
                        'defaults' => array('action' => 'login'),
                    ),
                    'may_terminate' => true, 
                ),
            ),
        ),
    ),
),

the following code in the file \module\Manager\view\manager\auth\login.phtml

<form class="form-horizontal" role="form" method="post" action="/login">
    <!-- code -->
    <div class="col-md-offset-2 col-md-8">
        <button type="submit" class="btn btn-block btn-primary">Login</button>
    </div>
</div>

If I try to access the first route (/tologinpage), it works,

but when i submit the form to controller AuthController\login, it is always show A 404 error occurred Page not found

Here is my controller

namespace Manager\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AuthController extends AbstractActionController
{

     public function showLoginAction()
     {
           $view = new ViewModel();

           $view->setTemplate('manager/auth/login');
           $view->setTerminal(true);  // close layout

           return $view;
      }


      public function loginAction()
      {
            var_dump($_POST);
            exit;
      }
}
1

1 Answers

2
votes

URL of loginAction isn't /login but /tologinpage/login, because login route is define as child of tologinpage route.

Btw, you shouldn't write URL by hand, but use URL view helper:

<form class="form-horizontal" role="form" method="post" action="<?php echo $this->url('auth/login'); ?>">

If you want make those roles on same level, put them on same level in configuration array:

    'router' => array(
        'routes' => array(
            'auth' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/tologinpage',
                    'defaults' => array(
                        'controller' => 'Manager\Controller\Auth',
                        'action'     => 'show-login',
                    ),
                ),
            ),
            'login'  => array(
                'type' => 'Literal',
                'options' => array(
                    'route'    => '/login',
                    'defaults' => array(
                        'controller' => 'Manager\Controller\Auth',
                        'action' => 'login',
                    ),
                ),
            ),
        ),
    ),