1

I have created an observer for customer_logout event and on its observer, I want to redirect the customer to a specific page. how can be done ? In Magento1.9.x as

   $observer->getControllerAction()->setRedirectWithCookieCheck($url);

I want replacement of above for the Magento2.x.

Thanks.

1 Answer 1

1
<?php
namespace HS\OneStepCheckout\Observer;

use Magento\Framework\Event\ObserverInterface;

class CheckoutCartAddProductComplete implements ObserverInterface
{
    protected $response;

    public function __construct(
        ..\\
        \Magento\Framework\App\ResponseInterface $response
    ) {
        ..\\
        $this->response = $response;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $url = "pass correct url here";
        $controller = $observer->getControllerAction();
        $this->response->setRedirect($url)->sendResponse();
        exit;
    }
}
15
  • got following error : PHP Fatal error: Uncaught Error: Call to a member function getResponse()
    – Manish
    Commented Jul 5, 2016 at 5:46
  • have you remove var folder? Commented Jul 5, 2016 at 5:47
  • No, should I need to recompile ??
    – Manish
    Commented Jul 5, 2016 at 5:48
  • Same issue, I think $controller doesnot get value from observer any alternate solution.
    – Manish
    Commented Jul 5, 2016 at 5:55
  • you can get hint from core file, magento2/vendor/magento/module-captcha/Observer/CheckUserLoginObserver.php Commented Jul 5, 2016 at 5:56

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.