回答:
キャッチしたい場合はcontroller_action_predispatch
、次のようにします。
app / code / Vendor / Module / etc / events.xml
<event name="controller_action_predispatch">
<observer name="check_login_persistent" instance="Vendor\Module\Observer\CheckLoginPersistentObserver" />
</event>
app / code / Vendor / Module / Observer / CheckLoginPersistentObserver.php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class CheckLoginPersistentObserver implements ObserverInterface
{
/**
* @var \Magento\Framework\App\Response\RedirectInterface
*/
protected $redirect;
/**
* Customer session
*
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\App\Response\RedirectInterface $redirect
) {
$this->_customerSession = $customerSession;
$this->redirect = $redirect;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$actionName = $observer->getEvent()->getRequest()->getFullActionName();
$controller = $observer->getControllerAction();
$openActions = array(
'create',
'createpost',
'login',
'loginpost',
'logoutsuccess',
'forgotpassword',
'forgotpasswordpost',
'resetpassword',
'resetpasswordpost',
'confirm',
'confirmation'
);
if ($controller == 'account' && in_array($actionName, $openActions)) {
return $this; //if in allowed actions do nothing.
}
if(!$this->_customerSession->isLoggedIn()) {
$this->redirect->redirect($controller->getResponse(), 'customer/account/login');
}
}
}
より最適化して機能するコードについては、以下の手順に従ってください。
イベントファイル@ app \ code \ Vendor \ Module \ etc \ frontend \ events.xmlを作成します
<?xml version='1.0'?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd'>
<event name='controller_action_predispatch'>
<observer
name='checklogin'
instance='Vendor\Module\Model\Observer'
/>
</event>
</config>
オブザーバーファイルapp \ code \ Vendor \ Module \ Model \ Observer.phpを作成します
namespace Vendor\Module\Model;
class Observer implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
# check if user is logged in
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if(!$customerSession->isLoggedIn())
{
$request = $objectManager->get('Magento\Framework\App\Request\Http');
//get instance for URL interface
/** @var \Magento\Framework\UrlInterface $urlInterface */
$urlInterface = $objectManager->get('Magento\Framework\UrlInterface');
// URL to redirect to
$url = $urlInterface->getUrl('customer/account/login');
if(strpos($request->getPathInfo(), '/customer/account/') !== 0)
{
# redirect to /customer/account/login
$observer->getControllerAction()
->getResponse()
->setRedirect($url);
}
}
}
if ($controller == 'account' && in_array($action, $openActions)) { return $this; //if in allowed actions do nothing. }
このコードは決して実行されず、コードにアクション名の付いた変数はありません。また、__constructに(あなたが「」最後にどのエラーにリードを配置。