1

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);
    }

}
  • Can you add your controller and helper file to your question? – Sukumar Gorai Aug 1 '18 at 14:39
  • I have edited my post and attached source code of controller and helper – Michał Paszowski Aug 1 '18 at 14:52
  • Why not you have injected the cart, session etc to your controller instead of helper? If you also need those in helper then you need to inject those both in controller and in helper. This will solve your issue. – Sukumar Gorai Aug 1 '18 at 14:57
0

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;
    }
}
  • Thats great, what about using scope config?: $this->scopeConfig->getValue(self::something, \Magento\Store\Model\ScopeInterface::SCOPE_STORE – Michał Paszowski Aug 1 '18 at 15:14
  • Check my updated answer. – Sukumar Gorai Aug 1 '18 at 15:19
  • 1
    Unfortunately I am getting this exception Uncaught ArgumentCountError: Too few arguments to function _construct – Michał Paszowski Aug 2 '18 at 7:21
  • When are you getting this error? During di compile? – Sukumar Gorai Aug 2 '18 at 7:23
  • I was trying to call constructor without compiling di, thank you so much! – Michał Paszowski Aug 2 '18 at 8:10
0
       <?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;
  }
}

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.