Logout.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Ajax;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. /**
  10. * Logout controller
  11. *
  12. * @method \Magento\Framework\App\RequestInterface getRequest()
  13. * @method \Magento\Framework\App\Response\Http getResponse()
  14. */
  15. class Logout extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
  16. {
  17. /**
  18. * @var \Magento\Customer\Model\Session
  19. */
  20. protected $customerSession;
  21. /**
  22. * @var \Magento\Framework\Controller\Result\JsonFactory
  23. */
  24. protected $resultJsonFactory;
  25. /**
  26. * Initialize Logout controller
  27. *
  28. * @param \Magento\Framework\App\Action\Context $context
  29. * @param \Magento\Customer\Model\Session $customerSession
  30. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  31. */
  32. public function __construct(
  33. \Magento\Framework\App\Action\Context $context,
  34. \Magento\Customer\Model\Session $customerSession,
  35. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  36. ) {
  37. parent::__construct($context);
  38. $this->customerSession = $customerSession;
  39. $this->resultJsonFactory = $resultJsonFactory;
  40. }
  41. /**
  42. * Customer logout action
  43. *
  44. * @return \Magento\Framework\Controller\Result\Json
  45. */
  46. public function execute()
  47. {
  48. $lastCustomerId = $this->customerSession->getId();
  49. $this->customerSession->logout()
  50. ->setBeforeAuthUrl($this->_redirect->getRefererUrl())
  51. ->setLastCustomerId($lastCustomerId);
  52. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  53. $resultJson = $this->resultJsonFactory->create();
  54. return $resultJson->setData(['message' => 'Logout Successful']);
  55. }
  56. }