Question

I have helper class that has constructor that takes following parameters: Context, Cart, Session, CollectionFactory. It derives from \Magento\Framework\App\Helper\AbstractHelper.

I would like to use this helper inside of my frontend controller but I don't know what to do to make my controller constructor called with extra parameters so that I could use them to initialize my helper class.

Point is - I would like to have access to Context, Cart, Session, CollectionFactory in controller that derives from \Magento\Framework\App\Action\Action.

Here is source code of my controller and part of the helper class:

<?php
namespace Something\Something\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {

        echo $some_json_that_i_will_use_in_the_frontend;
    }
}

Helper:

<?php

namespace Something\Something\Helper;

use \Magento\Customer\Model\Session;
use \Magento\Checkout\Model\Cart;
use \Magento\Framework\App\Helper\Context;
use \Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use \Magento\Sales\Model\Order;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    private $cart;

    private $customerSession;

    private $orderCollectionFactory;

    public function __construct(
        Context $context, Cart $cart, Session $customerSession, CollectionFactory $orderCollectionFactory
    ) {
        $this->cart = $cart;
        $this->customerSession = $customerSession;
        $this->orderCollectionFactory = $orderCollectionFactory;

        parent::__construct($context);
    }

}
Was it helpful?

Solution

You can use the below code for controller file:

<?php
namespace Something\Something\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    protected $_cart;
    protected $_customerSession;
    protected $_orderCollectionFactory;
    protected $_scopeConfig;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Checkout\Model\Cart $cart, 
        \Magento\Customer\Model\Session $customerSession, 
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ){
        $this->_pageFactory = $pageFactory;
        $this->_cart = $cart;
        $this->_customerSession = $customerSession;
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->_scopeConfig = $scopeConfig;
        return parent::__construct($context);
    }

    public function execute()
    {

        echo $some_json_that_i_will_use_in_the_frontend;
    }
}

OTHER TIPS

       <?php
    namespace Something\Something\Controller\Index;

  class Index extends \Magento\Framework\App\Action\Action
  {
    protected $_pageFactory;
    protected $helper;

    public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Something\Something\Helper $helper
    \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
      $this->helper = $helper;
      $this->_pageFactory = $pageFactory;
      return parent::__construct($context);
    }

  public function execute()
  {

    echo $some_json_that_i_will_use_in_the_frontend;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top