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);
 }
}
asked Aug 1, 2018 at 14:19
3
  • Can you add your controller and helper file to your question? Commented Aug 1, 2018 at 14:39
  • I have edited my post and attached source code of controller and helper Commented Aug 1, 2018 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. Commented Aug 1, 2018 at 14:57

2 Answers 2

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.