Magento – Magento2 : Redirect Using Plugin (With Website Restrictions ON)

magento-enterprisemagento2pluginwebsite-restriction

Postlogin.php

<?php
namespace Mycompany\Customerhomepage\Plugin;
use Magento\Framework\Controller\ResultFactory;

class Postlogin
{
    public function __construct(
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $response,
        ResultFactory $resultFactory
    ) {
        $this->_url = $url;
        $this->_response = $response;
        $this->resultFactory = $resultFactory;
    }

    public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject , $result)
    {
        $customerBeforeAuthUrl = $this->_url->getUrl('customerhomepage/index/landing');
        $this->_response->setRedirect($customerBeforeAuthUrl)->sendResponse();
    }
}

    

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Customer\Controller\Account\LoginPost">
       <plugin name="Mycompany_Plugin_Postlogin" type="Mycompany\Customerhomepage\Plugin\Postlogin"></plugin>
   </type>
</config>

I want to redirect to a custom page after login , how to do that by plugin ?

UPDATE

The Code Works fine it redirects to Custom page , as soon as the website restrictions are on it stop working

Best Answer

You can try like below

use Magento\Framework\App\Response\Http as responseHttp;
use Magento\Framework\UrlInterface;

class Test{

    public function __construct(
        responseHttp $response,
        UrlInterface $url
    )
    {
        $this->response = $response;
        $this->_url = $url;
    }

    public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject , $result)    {
         $url = $this->_url->getUrl('customerhomepage/index/landing');
         $this->response->setRedirect($url);
         return $result;
    }
}
Related Topic