注销不起作用

时间:2016-08-26 01:39:42

标签: zend-framework2

我制作了登录/注册模块。我还将LoginController中的indexAction更改为loginAction,但Logout仍无法正常工作。我尽我所能,但我不知道问题是什么。 我的代码如下:

LoginController.php

namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Users\Form\LoginForm;
use Users\Form\LoginFilter;
use Users\Model\User;
use Users\Model\UserTable;
class LoginController extends AbstractActionController
{
  protected $storage;
  protected $authservice;

  public function getAuthService()
  {
     if (! $this->authservice) {
     $this->authservice = $this->getServiceLocator()->get('AuthService');
     }
    return $this->authservice;
  }

public function indexAction()
{
    $this->layout("layout/layout_users");  

    if ($this->getAuthService()->hasIdentity())       
    {                            
        return $this->redirect()->toRoute('admin',           
        array('action'=>'index'));         
    }

    $form = $this->getServiceLocator()->get('LoginForm');

    $viewModel  = new ViewModel(array('form' => $form)); 
    return $viewModel;      
}

public function processAction()
{
    if (!$this->request->isPost()) {
        return $this->redirect()->toRoute(NULL , array( 
                    'controller' => 'login', 
                    'action' =>  'index' 
                ));
    }

    $post = $this->request->getPost();

    $form = new LoginForm();
    $inputFilter = new LoginFilter();
    $form->setInputFilter($inputFilter);

    $form->setData($post);
    if (!$form->isValid()) {
        $model = new ViewModel(array(
            'error' => true,
            'form'  => $form,
        ));
        $model->setTemplate('users/login/index');
        return $model;
    } else {
        //check authentication...
        $this->getAuthService()->getAdapter() 

       ->setIdentity($this->request->getPost('email'))             
       ->setCredential($this->request->getPost('password'));

        $result = $this->getAuthService()->authenticate();
        if ($result->isValid()) {

$this->getAuthService()->getStorage()->write($this->request->getPost('email'));
            return $this->redirect()->toRoute('admin' , array(  ));

        } else {
            $model = new ViewModel(array(
                'error' => true,
                'form'  => $form,
            ));
            $model->setTemplate('users/login/index');
            return $model;
        }


    }
}

public function logoutAction() 
{
    if ($this->getAuthService()->hasIdentity()) { 
        $this->getAuthService()->clearIdentity(); 
    } 
    return $this->redirect()->toRoute('users/login',        
    array('action'=>'index'));  
}



}

用户模块的module.config.php

<?php
return array(
'controllers' => array(
    'invokables' => array( 
        'Users\Controller\Register' => 'Users\Controller\RegisterController',
        'Users\Controller\Login' => 'Users\Controller\LoginController',             
    ),
),

 'router' => array(
    'routes' => array(
        'users' => array(
            'type'    => 'Literal',
            'options' => array( 
                'route'    => '/users',                 
                'defaults' => array(
                    'controller' => 'Users\Controller\Login',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(                     
                'login' => array(
                    'type'    => 'Segment',
                    'may_terminate' => true,
                    'options' => array(
                        'route'    => '/login[/:action]',
                        'constraints' => array(
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'controller' => 'Users\Controller\Login',
                            'action'     => 'index',
                        ),                              
                    ),
                  ),

                 'register' => array(
                    'type'    => 'Segment',
                    'may_terminate' => true,
                    'options' => array(
                        'route'    => '/register[/:action]',
                        'constraints' => array(
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'controller' => 'Users\Controller\Register',
                            'action'     => 'index',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

'view_manager' => array(
    'template_map' => array(
        'layout/layout_users'  => __DIR__ . '/../view/layout/layout.phtml',
     ),

    'template_path_stack' => array(
        'users' => __DIR__ . '/../view',
    ),
),


);
管理模块的

module.config.php:

<?php 

namespace Admin;

return array(
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Admin\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),



       'profile' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array(
                'route' => '/profile[/:action]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'Admin\Controller\Profile',
                    'action' => 'index',
                ),
            ),
        ),

       'logout' => array(
            'type' => 'Segment',
            'may_terminate' => true,
            'options' => array(
                'route' => '/users/login',
                'defaults' => array(
                    'controller' => 'Users\Controller\Login',
                    'action' => 'logout',
                ),
            ),
        ),

'admin' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/admin',
                'defaults' => array(
                    '__NAMESPACE__' => 'Admin\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

 'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'factories' => array(
        'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
        'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

'controllers' => array(
    'invokables' => array(
      // 'Admin\Controller\Index' => Controller\IndexController::class
        'Admin\Controller\Index' => 'Admin\Controller\IndexController',
        'Admin\Controller\Profile' => 'Admin\Controller\ProfileController',

        'Admin\Controller\Provinces' => 'Admin\Controller\ProvincesController',
        'Admin\Controller\Districts' => 'Admin\Controller\DistrictsController',
        'Admin\Controller\Cities' => 'Admin\Controller\CitiesController',
        'Admin\Controller\Stations' => 'Admin\Controller\StationsController',           
        'Admin\Controller\Services' => 'Admin\Controller\ServicesController',
        'Admin\Controller\Vehicles' => 'Admin\Controller\VehiclesController', 

    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(


        'layout/layout_admin'           => __DIR__ . '/../view/layout/layout.phtml',
        'admin/index/index' => __DIR__ . '/../view/admin/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

// Placeholder for console routes
'console' => array(
    'router' => array(
        'routes' => array(
        ),
    ),
),

'navigation' => array(
    'default' => array(
        array(
            'label' => 'Home',
            'route' => 'home',
        ),

        array(
            'label' => 'Profile',
            'route' => 'profile',
        ),

        array(
            'label' => 'Logout',
            'route' => 'logout',
        ),

    ),  
),
);

用户模块中的视图目录是: 视图/用户/登录/ index.phtml

1 个答案:

答案 0 :(得分:2)

您在路线和控制器中都有错误。

在控制器中

public function logoutAction() 
{
    if ($this->getAuthService()->hasIdentity()) { 
        $this->getAuthService()->clearIdentity(); 
    } 
    return $this->redirect()->toRoute('users/login', //Change to 'home'
    array('action'=>'index'));  
}

在module.config

'logout' => array(
    'type' => 'Segment',
    'may_terminate' => true,
    'options' => array(
        'route' => '/users/login', //Change to '/users/logout'
        'defaults' => array(
            'controller' => 'Users\Controller\Login',
            'action' => 'logout',
        ),
    ),
),