0

I have already implemented This solution , but it is not working for me

I am trying to redirect customer after login to different page rather than my account

What i tried

\app\code\Mycompany\Customerhomepage\etc\frontend\events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/framework/Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="mycompany_customerhomepage_customer_login" instance="Mycompany\Customerhomepage\Observer\Customerlogin"/>
    </event>
</config>

\app\code\Mycompany\Customerhomepage\Observer\Customerlogin.php

<?php

namespace Mycompany\Customerhomepage\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\ResponseFactory;

/**
 * Class customerlogin
 * @package Mycompany\Customerhomepage\Observer
 */
class customerlogin implements ObserverInterface{
    protected $_responseFactory;
    protected $_url;

    /**
     * @param UrlInterface $url
     * @param ResponseFactory $responseFactory
     */
    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
    }
    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $customerBeforeAuthUrl = $this->_url->getUrl('customerhomepage/index/landing');
        $this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse();
        die();

    }
}

It also exhibiting in variable behavior , sometime it redirects sometime it does not

1

1 Answer 1

1

Using ResponseInterface did the trick for me

namespace Mycompany\Customerhomepage\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\UrlInterface;

/**
 * Class customerlogin
 * @package Mycompany\Customerhomepage\Observer
 */
class customerlogin implements ObserverInterface{
    protected $_responseFactory;
    protected $_url;

    /**
     * @var \Magento\Framework\App\ResponseInterface
     */
    protected $_response;
    /**
     * @param UrlInterface $url
     * @param ResponseFactory $responseFactory
     */
    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->_url = $url;
        $this->_response = $response;

    }
    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $customerBeforeAuthUrl = $this->_url->getUrl('customerhomepage/index/landing');

        $this->_response->setRedirect($customerBeforeAuthUrl)->sendResponse();
        exit(0);
    }
}
2
  • You can remove \Magento\Framework\App\ResponseFactory $responseFactory as that's not being used in the Observer. Feb 7, 2017 at 7:33
  • 2
    The solution didn't work for me. Feb 7, 2017 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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