Created
January 18, 2018 16:21
-
-
Save LordZardeck/382686e50eea0e76125c5e0be755a020 to your computer and use it in GitHub Desktop.
Reference to how to prevent access to a page for guests in Magento 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Magento\Customer\Model\Session as CustomerSession; | |
use Magento\Framework\App\Action\Action; | |
use Magento\Framework\App\Action\Context; | |
use Magento\Framework\App\RequestInterface; | |
use Magento\Framework\Controller\ResultFactory; | |
class Index extends Action | |
{ | |
/** | |
* @var CustomerSession | |
*/ | |
protected $customerSession; | |
/** | |
* @param CustomerSession $customerSession | |
* @param Context $context | |
*/ | |
public function __construct(CustomerSession $customerSession, Context $context) | |
{ | |
parent::__construct($context); | |
$this->customerSession = $customerSession; | |
} | |
/** | |
* {@inheritDoc} | |
* @throws \InvalidArgumentException | |
*/ | |
public function execute() | |
{ | |
/** @var \Magento\Framework\View\Result\Page $resultPage */ | |
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);= | |
} | |
/** | |
* Check customer authentication | |
* | |
* @param RequestInterface $request | |
* | |
* @return \Magento\Framework\App\ResponseInterface | |
* @throws \Magento\Framework\Exception\NotFoundException | |
*/ | |
public function dispatch(RequestInterface $request) | |
{ | |
if (!$this->customerSession->authenticate()) { | |
$this->_actionFlag->set('', 'no-dispatch', true); | |
} | |
return parent::dispatch($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment