0

I am not able to pass variables to controller from my observer file. Below is the code for observer:

 class Sendtogateway implements \Magento\Framework\Event\ObserverInterface
        {
          protected $_responseFactory;
          protected $_url;
          protected $order;
            protected $logger;

            public function __construct(
                \Magento\Framework\App\ResponseFactory $responseFactory,
            \Magento\Framework\UrlInterface $url,
            \Magento\Sales\Api\Data\OrderInterface $order,
                \Psr\Log\LoggerInterface $loggerInterface
            ){
                $this->_responseFactory = $responseFactory;
            $this->_url = $url;
            $this->order = $order;
                $this->logger = $loggerInterface;
            }

          public function execute(\Magento\Framework\Event\Observer $observer)
          {
            $id = $observer->getEvent()->getOrder()->getIncrementId();
            $orderdetail = $this->order->loadByIncrementId($id);
            $url = "affrikpay/index/index/id/".$id;
            $customerBeforeAuthUrl = $this->_url->getUrl($url);
            $this->_responseFactory->create()->setRedirect($customerBeforeAuthUrl)->sendResponse();
            $this->logger->debug('done');
          }

        }

And calling variable in controller with

     public function execute()
        {      
           $id = $this->request->getParam('id');      
           $this->logger->debug($id);
        }
2
  • You shouldn't call a controller in your observer. That might be the cause of your problem. Can you give me more context of the exact problem? Nov 18, 2017 at 15:04
  • Thanks for your interest David , I am able to redirect to controller from observer but was not able to send the order id. Nov 20, 2017 at 7:36

1 Answer 1

1

Getting variable from param is not a reliable way, you should try using session, since you seem to be dealing with checkout use checkout session like following ;

Inject \Magento\Checkout\Model\Session into observer's constructor and set your variable like ;

$this->_checkoutSession->setVariableName('value');

you can get it on controller like following ;

<?php

namespace Vendor\Module\Controller\Demo;


class Demo extends \Magento\Framework\App\Action\Action
{

    protected $_checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {

        $this->_checkoutSession = $checkoutSession;
    }


    public function execute()
    {
        $varValue = $this->_checkoutSession->getVariableName();
    }
}

EDIT :

If you do want to do it using params , you are passing them wrongly, remove $url = "affrikpay/index/index/id/".$id; and use following in place of $customerBeforeAuthUrl = $this->_url->getUrl($url);

$query = ['id' => $id];
$customerBeforeAuthUrl = $this->_url->getUrl('affrikpay/index/index',['_use_rewrite' => true, '_query' => $query]);
5
  • Thanks Neo, well I am using place order after observer so I think checkout session will be lost after place order. However I am able to get last order id in observer. I am trying to pass this order id to controller. Though checkout session I am not sure will work but yeah session is a good idea I will create my on session variable. Thanks for guiding me Nov 20, 2017 at 6:15
  • I have used session in a similar situation, so I suggested this. Nov 20, 2017 at 6:24
  • @AshishSharma please check updated answer Nov 20, 2017 at 6:40
  • I just want to confirm I am using observer to call the controller and send order detail from controller to payment gateway. Is observer a good choice or we can call controller with order id directly from knockout js etc. if there is any possibility please help :) Nov 20, 2017 at 7:02
  • session brought in value to controller thanks so much. U r champ 100 likes Nov 20, 2017 at 7:30

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.