I want to change default home page to login page in Magento 2.3
Mean that want to see customer login instead of homepage
Create Event controller_action_predispatch, we can follow:
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
/**
* @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');
}
}