Magento 2-如果未登录,则将用户重定向到特定页面


9

如果未登录,我需要将用户重定向到登录页面。在此链接中可以找到类似的作品。Magento 2有解决方案吗?

Answers:


21

如果我们想抓住controller_action_predispatch,我们可以遵循:

app / code / Vendor / Module / etc / events.xml


     <event name="controller_action_predispatch">
            <observer name="check_login_persistent" instance="Vendor\Module\Observer\CheckLoginPersistentObserver" />
     </event>

应用程序/代码/供应商/模块/观察者/CheckLoginPersistentObserver.php

namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;


class CheckLoginPersistentObserver implements ObserverInterface
{
         /**
         * @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');
            }

        }

}

2
if ($controller == 'account' && in_array($action, $openActions)) { return $this; //if in allowed actions do nothing. }此代码从未执行过,在代码中没有带名称操作的变量。也__construct(你放置一个“”年底从而导致错误。
阿希什Madankar M2位专业人员

1
您如何为此参考特定的控制器?我已经复制了代码,但是不太了解如何在按下控制器时将其触发。
哈里

4

有一个更简单的解决方案。看一下这个文件:

src /供应商/magento/module-sales/etc/di.xml

<type name="Magento\Sales\Controller\Order\History">
    <plugin name="authentication" type="Magento\Sales\Controller\Order\Plugin\Authentication"/>
</type>

因此,您只需要在模块di.xml中使用身份验证插件


4

要获得更多优化和工作代码,您可以按照以下步骤操作。

  1. 创建事件文件@ app \ code \ Vendor \ Module \ etc \ frontend \ events.xml

    <?xml version='1.0'?>
    <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd'>
        <event name='controller_action_predispatch'>
            <observer
                    name='checklogin'
                    instance='Vendor\Module\Model\Observer'
            />
        </event>
    </config>
    
  2. 创建观察者文件app \ code \ Vendor \ Module \ Model \ Observer.php

    namespace Vendor\Module\Model;
    
    class Observer implements \Magento\Framework\Event\ObserverInterface
    {
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        # check if user is logged in
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        if(!$customerSession->isLoggedIn())
        {
            $request = $objectManager->get('Magento\Framework\App\Request\Http');
            //get instance for URL interface
            /** @var \Magento\Framework\UrlInterface $urlInterface */
            $urlInterface = $objectManager->get('Magento\Framework\UrlInterface');
            // URL to redirect to
            $url = $urlInterface->getUrl('customer/account/login');
            if(strpos($request->getPathInfo(), '/customer/account/') !== 0)
            {
                # redirect to /customer/account/login
                $observer->getControllerAction()
                    ->getResponse()
                    ->setRedirect($url);
            }
        }
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.