Magento 2: Перенаправление с домашней страницы на вход, если он не зарегистрирован
Я хотел бы перенаправить клиента с домашней страницы на страницу входа, если он не вошел в систему.
У меня есть модуль, однако он просто показывает пустую домашнюю страницу вместо перенаправления.
Репо: 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;
}
}
2
1 answers
Попробуйте сделать это следующим образом:
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); } } }
Очистить кэш.
3
Author: Sohel Rana, 2017-01-27 15:44:38