Zend framework2 注销不起作用
我制作了登录/注册模块。我还将LoginController中的indexAction更改为loginAction,但仍然无法注销。我尽了最大的努力,但我不知道问题出在哪里。 我的代码如下: LoginController.phpZend framework2 注销不起作用,zend-framework2,Zend Framework2,我制作了登录/注册模块。我还将LoginController中的indexAction更改为loginAction,但仍然无法注销。我尽了最大的努力,但我不知道问题出在哪里。 我的代码如下: LoginController.php namespace Users\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; use Zend\Authentication\A
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 for Users模块
<?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',
),
),
);
您的路线和控制器有错误
内部控制器
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',
),
),
),
您在路线和控制器中有错误
内部控制器
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',
),
),
),
非常感谢你的朋友,问题解决了。现在它可以正常工作了。@AyazKhan欢迎您。请将我的答案设置为最佳答案。请验证答案,以便主题接近。非常感谢您的朋友,问题已经解决。现在它可以正常工作了。@AyazKhan欢迎您。请将我的答案设置为最佳。请验证答案,以便关闭主题