I would like to redirect customer from homepage to login page, if they are not logged in.
I have a module however this just shows a blank homepage instead of redirecting.
Repo: https://github.com/BHWD/homeredirect
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE,
'iuk_homeredirect',
__DIR__ );
etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="iuk_homeredirect" setup_version="1.0.0">
</module>
</config>
etc/di.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Cms\Controller\Index\Index" type="iuk\homeredirect\Controler\Index\Redirecthome" />
</config>
Controler/Index/Redirecthome.php
<?php
namespace iuk\homeredirect\Controller\Index;
class Redirecthome extends \Magento\Framework\App\Action\Action
{
protected $resultForwardFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
) {
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($context);
}
public function execute($coreRoute = null)
{
$this->_redirect('customer/account/login');
return;
}
}
-
This is simplest solution i found. magecomp.com/magento-2-custom-redirect.htmlGaurav Jain– Gaurav Jain2017年08月15日 09:07:48 +00:00Commented Aug 15, 2017 at 9:07
1 Answer 1
Try following way:
Iuk/Homeredirect/etc/frontend/events.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="controller_action_predispatch"> <observer name="sr_controller_action_predispatch" instance="Iuk\Homeredirect\Observer\ControllerPredispatch" /> </event> </config>
Iuk/Homeredirect/Observer/ControllerPredispatch.php
<?php
namespace Iuk\Homeredirect\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
class ControllerPredispatch implements ObserverInterface
{
/**
* @var \Magento\Framework\UrlInterface
*/
protected $url;
/**
* @var \Magento\Framework\App\Response\Http
*/
protected $http;
/** @var \Magento\Customer\Model\Session */
protected $customerSession;
/**
* @param \Magento\Framework\UrlInterface $url
* @param \Magento\Framework\App\Response\Http $http
* @param \Magento\Customer\Model\Session $customerSession
*/
public function __construct(
\Magento\Framework\UrlInterface $url,
\Magento\Framework\App\Response\Http $http,
\Magento\Customer\Model\Session $customerSession
)
{
$this->url = $url;
$this->http = $http;
$this->customerSession = $customerSession;
}
/**
* Manages redirect
*/
public function execute(Observer $observer)
{
/**
* Check if user logged in
*/
if ($this->customerSession->isLoggedIn()) {
return;
}
if($observer->getRequest()->getFullActionName() == 'cms_index_index') {
/**
* Redirect to login
*/
$this->http->setRedirect($this->url->getUrl('customer/account/login'), 301);
}
}
}
Clear cache.
-
I have added this in app/code and I added
'SR_StackExchange' => 1to my config.php file however it does not show up in the list when I runsetup:upgradeBen H– Ben H2017年01月27日 15:29:09 +00:00Commented Jan 27, 2017 at 15:29 -
Hey no need to do this. You can just adjust with your module. Please replace SR\StackExchange to Iuk\HomeredirectSohel Rana– Sohel Rana2017年01月27日 15:30:20 +00:00Commented Jan 27, 2017 at 15:30
-
Still see SR/StackExchange answer?Ben H– Ben H2017年01月27日 15:31:52 +00:00Commented Jan 27, 2017 at 15:31
-
Your module Iuk\Homeredirect, right?Sohel Rana– Sohel Rana2017年01月27日 15:32:35 +00:00Commented Jan 27, 2017 at 15:32
-
Yes that is correct iuk\homeredirectBen H– Ben H2017年01月27日 15:32:58 +00:00Commented Jan 27, 2017 at 15:32
Explore related questions
See similar questions with these tags.