Question

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;
    }
}
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top