1

I've following controller code which is working fine using object manager. How I can use constructor injection instead of object manager for this ?

namespace Amit\Qr\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/** @var \Magento\Framework\View\Result\PageFactory  */

    protected $resultPageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory

    ) {
        $this->resultPageFactory = $resultPageFactory;

        parent::__construct($context);
    }

    public function execute()
    {
        $orderId = $this->getRequest()->getParam('order_id');
        $order = $this->_objectManager->create('Magento\Sales\Model\Order');
        $order->load($orderId);
        $customerId = $order->getCustomerId();
        echo $customerId;
        //.....
   }
}
5

Just move all dependencies to constructor

class Index extends \Magento\Framework\App\Action\Action
{
    /** @var \Magento\Framework\View\Result\PageFactory  */
    protected $resultPageFactory;
    private $orderFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Sales\Model\OrderFactory $orderFactory

    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->orderFactory = $orderFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $orderId = $this->getRequest()->getParam('order_id');
        $order = $this->orderFactory->create();
        $order->load($orderId);
        $customerId = $order->getCustomerId();
        echo $customerId;
        //.....
   }
}

next step will be use Services:

class Index extends \Magento\Framework\App\Action\Action
{
    /** @var \Magento\Framework\View\Result\PageFactory  */
    protected $resultPageFactory;
    /** @var \Magento\Sales\Model\OrderRepository */
    private $orderRepository;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Sales\Model\OrderRepository $orderRepository

    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->orderRepository = $orderRepository;
        parent::__construct($context);
    }

    public function execute()
    {
        $orderId = $this->getRequest()->getParam('order_id');
        $order = $this->orderRepository->get($orderId);
        $customerId = $order->getCustomerId();
        echo $customerId;
        //.....
   }
}
  • Controller code works. How I can use it with services? – amitshree Mar 20 '16 at 10:33
  • 1
    After add dependencies You should remove var/generation folder – Marcin Korzystka Jul 6 '17 at 10:02
2

You have to inject Model Order by Factories

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Model\OrderFactory $orderFactory
    .....another injection......
) {
    $this->orderFactory = $orderFactory;
    ....
    parent::__construct($context);
}


public function execute() {
     $order = $this->orderFactory->create(); // Use Factory to create instance order model object
}

Try to avoid use directly _objectManager as much as possible

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.