I am trying to access the session so I can redirect if the user is not logged in. Used what should be correct, from several sources:
protected $_session;
public function __construct(\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Backend\Model\Auth\Session $authSession)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->_session = $authSession;
}
However I get the message on page load:
Fatal error: Uncaught TypeError: Argument 3 passed to
\Controller\Index\Index::__construct() must be an instance of Magento\Backend\Model\Auth\Session, none given, called in ..\wwwroot\var\generation....\Controller\Index\Index\Interceptor.php on line 14 and defined in ..\wwwroot\app\code....\Controller\Index\Index.php on line 15
Why is this? Have I missed something obvious (as far as I can work out this works fine on a block I have - but not on this controller and I can't see any difference.
Edit: full code Differs slightly from above because I've tried a couple of things since.
<?php
namespace ..\Subscription\Controller\Index;
use \Magento\Framework\App\Action\Action;
class Index extends Action implements \Magento\Framework\DataObject\IdentityInterface
{
/** @var \Magento\Framework\View\Result\Page */
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $sessionSession
*/
public function __construct(\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Customer\Model\Session $sessionSession)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->_customerSession = $sessionSession;
}
/**
* Subscription Index, shows overview of their subscriptions.
*
* @return \Magento\Framework\View\Result\PageFactory
*/
public function execute()
{
// Check the user is logged in
if($this->_customerSession->isLoggedIn()){
return $this->resultPageFactory->create();
}
else {
return $this->_redirect('customer/account/login');
}
}
/**
* Return identifiers for produced content
*
* @return array
*/
public function getIdentities()
{
return [\..\Subscription\Model\Post::CACHE_TAG . '_' . 'list'];
}
}