3

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

3
  • Please provide more information and not just code. What happens with your current code? Did you make sure that your plugin is executed? Does it throw an error? Commented Sep 6, 2016 at 14:27
  • @fschmengler I found the issue , the code is working fine . but as soon as i turn on website restriction the page is redirected to My account page rather than custom page , how should i fix that. without website restriction it is working fine Commented Sep 6, 2016 at 14:31
  • I don't know how website restriction works but it might use a similar plugin that is executed before yours. Commented Sep 6, 2016 at 15:21

2 Answers 2

7

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;
    }
}
2
  • 2
    For anyone else looking for a solution to before plugin, you should return $subject instead of $result
    – Wildcard27
    Commented May 2, 2017 at 1:18
  • @Wildcard27 if there is around plugin then what we have to use? Commented Jan 3, 2019 at 11:58
3

Thats what worked for me :

public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject, $result)
{
    $result->setPath('home-page'); // Change this to what you want
    return $result;
}
2
  • It works. Simple solution :) Commented Jun 18, 2018 at 12:35
  • Its not working Commented Aug 9, 2018 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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