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– Sukumar Gorai2018年08月01日 14:39:08 +00:00Commented Aug 1, 2018 at 14:39
-
I have edited my post and attached source code of controller and helperMister Paszczakowski– Mister Paszczakowski2018年08月01日 14:52:42 +00:00Commented 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.Sukumar Gorai– Sukumar Gorai2018年08月01日 14:57:39 +00:00Commented Aug 1, 2018 at 14:57
2 Answers 2
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_STOREMister Paszczakowski– Mister Paszczakowski2018年08月01日 15:14:51 +00:00Commented Aug 1, 2018 at 15:14 -
Check my updated answer.Sukumar Gorai– Sukumar Gorai2018年08月01日 15:19:58 +00:00Commented Aug 1, 2018 at 15:19
-
1Unfortunately I am getting this exception
Uncaught ArgumentCountError: Too few arguments to function _constructMister Paszczakowski– Mister Paszczakowski2018年08月02日 07:21:24 +00:00Commented Aug 2, 2018 at 7:21 -
When are you getting this error? During di compile?Sukumar Gorai– Sukumar Gorai2018年08月02日 07:23:13 +00:00Commented Aug 2, 2018 at 7:23
-
I was trying to call constructor without compiling di, thank you so much!Mister Paszczakowski– Mister Paszczakowski2018年08月02日 08:10:29 +00:00Commented Aug 2, 2018 at 8:10
<?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;
}
}