2016-08-26 4 views
0

로그인/등록 모듈을 만들었습니다. LoginController의 indexAction을 loginAction으로 변경했지만 여전히 로그 아웃이 작동하지 않습니다. 최선을 다하지만 문제가 무엇인지 알지 못합니다. 내 코드는 아래와 같습니다 :로그 아웃이 작동하지 않습니다.

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', 
     ), 

    ), 
), 
); 

사용자 모듈의보기 용 디렉토리는 다음과 같습니다. view/users/login/in dex.phtml

답변

2

경로와 컨트롤러에 실수가 있습니다. 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', 
     ), 
    ), 
), 
+0

에서 컨트롤러

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

에서

가 대단히 친구를 감사, 문제는 해결된다. 이제 제대로 작동합니다. –

+0

@AyazKhan 환영합니다. 내 대답을 최고로 설정하십시오. – newage

+0

제목이 닫히도록 답변을 확인하십시오. – Unex

관련 문제