3

In Magento 2, I want to write a custom controller. Whenever anyone tries to call the controller

URL (http://mymagento.com/mycontroller/index/index).

It should redirect to this controller an event dispatch should trigger to logout from Magento 2 site.

5 Answers 5

5

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.

3
  • It's showing invalid _logout() method Jul 26, 2019 at 6:48
  • 1
    Never use $objectManager. It has already been deprecated. And It will make your website slow... Always define your class in __construct method and use it. Jul 26, 2019 at 7:44
  • yes, i agree when you are working testing than its okay after than you configure in construct. Jul 26, 2019 at 9:08
3

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.

2

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.

2

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

3
  • Thanks, MSA. I was looking exactly this solution. It should destroy the session of the customer and redirect to a different page. Jul 26, 2019 at 7:39
  • Yes, you can change to different page @Arshad Syed Jul 26, 2019 at 7:50
  • Need to change code here.. $result->setUrl($this->url->getUrl('route_name/controller/action ')); Jul 26, 2019 at 7:51
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.