In Magento 2, I want to write a custom controller. Whenever anyone tries to call the controller
It should redirect to this controller an event dispatch should trigger to logout from Magento 2 site.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIn Magento 2, I want to write a custom controller. Whenever anyone tries to call the controller
It should redirect to this controller an event dispatch should trigger to logout from Magento 2 site.
Please try with below code.
<?php
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Session $customerSession
) {
$this->redirect = $context->getRedirect();
$this->customerSession = $customerSession;
}
public function customerLogout() {
$customerId = $this->customerSession->getId();
if($customerId) {
$this->customerSession->logout()
->setBeforeAuthUrl($this->redirect->getRefererUrl())
->setLastCustomerId($customerId);
return "logout successfully";
}
}
than push basic commands.
I hope its work for you.
You can use this below code in your controller file.
protected $session;
public function __construct(
....
\Magento\Customer\Model\Session $session
....
){
....
$this->session = $session;
....
}
public function execute(){
$lastCustomerId = $this->session->getId();
$this->session->logout()->setBeforeAuthUrl($this->_redirect->getRefererUrl())->setLastCustomerId($lastCustomerId);
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('customer/account/logoutSuccess/');
return $resultRedirect;
}
Hope it maybe helpful for you.
Try this code
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Session $customerSession
) {
$this->redirect = $context->getRedirect();
$this->customerSession = $customerSession;
}
public function customerLogout() {
$customerId = $this->customerSession->getId();
if($customerId) {
$this->customerSession->logout()
->setBeforeAuthUrl($this->redirect->getRefererUrl())
->setLastCustomerId($customerId);
return "Customer logout successfully";
} else {
return "Customer is not login";
}
}
Hope it Helps.
Use Session destroy()
method. It's worked for me perfectly
use Magento\Customer\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\UrlInterface;
class Controllername()
{
//const MODULE_ENABLED = 'customerlogin/general/enable';
protected $coreRegistry;
protected $url;
protected $resultFactory;
protected $messageManager;
protected $session;
public function __construct(Registry $registry,
UrlInterface $url,
ManagerInterface $messageManager,
ScopeConfigInterface $scopeConfig,
Session $customerSession,
ResultFactory $resultFactory
)
{
$this->session = $customerSession;
$this->coreRegistry = $registry;
$this->url = $url;
$this->resultFactory = $resultFactory;
$this->messageManager = $messageManager;
$this->_scopeConfig = $scopeConfig;
}
public function Yourfunction()
{
$this->messageManager->getMessages(true);
// Adding a custom message
$this->messageManager->addErrorMessage(__('logout successfully.'));
// Destroy the customer session in order to redirect him to the login page
$this->session->destroy();
/** @var \Magento\Framework\Controller\Result\Redirect $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('customer/account/login'));
return $result;
}
Note:Code Tested
You can simply destroy session to logout the customer.
/** @var Magento\Customer\Model\Session; */
$session->destroy();
And than redirect page to login page.
/** @var \Magento\Framework\Controller\Result\Redirect $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl($this->url->getUrl('customer/account/login'));
return $result;
I wish it will helps you.