1

Admin login success observer redirect to my custom controller

   public function execute(\Magento\Framework\Event\Observer $observer) {

    //print_r($observer);
     $message = 'entered custom method';
    $this->_logger->info($message);
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

    // Your code

    $resultRedirect->setUrl($this->_redirect($this->_response, 'sample/test/index'));
    return $resultRedirect;
1
  • To which event did you write the observer?
    – Oshan
    Apr 5, 2017 at 6:45

2 Answers 2

4

USE OF DIE() AND exit on magento 2 is not good for code standard

You can do this some code.

add __construct() function at your Observer class.

and on this __construct() function inject two classes

  • \Magento\Framework\App\ResponseFactory

  • \Magento\Framework\UrlInterface $url

Then using setRedirect($url)->sendResponse() redirect to your custom controller

Your class can be:

<?php
namespace [Vendor]\[modulename]\Observer;
use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;

class [YourClass] implements ObserverInterface {
    protected $_responseFactory;
    protected $_url;

    public function __construct(

        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
    }

    public function execute(Observer $observer) {
             $event = $observer->getEvent();
             $RedirectUrl= $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
            $this->_responseFactory->create()->setRedirect($RedirectUrl)->sendResponse();
         die();
    }
}
3
  • sorry it redirects to dashboard Apr 28, 2016 at 11:54
  • @Amit Bera sir,how to redirect to product page using product url from observer $productUrl = $currentProduct->getProductUrl(); Feb 5, 2019 at 14:12
  • from where you want to redirect and also on which event you want to do that?
    – Amit Bera
    Feb 6, 2019 at 7:50
3

Can't use the die() method because of magento coding standard so i prefer below solution.

<?php
namespace [Vendor]\[modulename]\Observer;
use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
class [YourClass] implements ObserverInterface {

    protected $_redirect;
    protected $_url;
    public function __construct(
        ......

        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $redirect,
        ......
    ) {

        $this->_url = $url;
        $this->_redirect = $redirect;

    }
    public function execute(Observer $observer) {
             $event = $observer->getEvent();
             $CustomRedirectionUrl = $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
             $this->_redirect->setRedirect($CustomRedirectionUrl);

    }
}

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.