2

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;
    }
}
1

1 Answer 1

4

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.

16
  • I have added this in app/code and I added 'SR_StackExchange' => 1 to my config.php file however it does not show up in the list when I run setup:upgrade
    – Ben H
    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\Homeredirect
    – Sohel Rana
    Jan 27, 2017 at 15:30
  • Still see SR/StackExchange answer?
    – Ben H
    Jan 27, 2017 at 15:31
  • Your module Iuk\Homeredirect, right?
    – Sohel Rana
    Jan 27, 2017 at 15:32
  • Yes that is correct iuk\homeredirect
    – Ben H
    Jan 27, 2017 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.