I think my issue might have to do with dependency injection, which I'm still trying to grab the concept.
What I would like to do is to be able to get the URL using getUrl() function from Magento\Framework\UrlInterface object.
So this what I have so far.
- Create a functional controller and set up route. It is accessible via web browser and has
execute()function defined. - Add use
Magento\Framework\UrlInterface; in a controller class - Add a protected variable
$urlBuilder Add a constructor like below
public function __construct( UrlInterface $urlBuilder ) { $this->urlBuilder = $urlBuilder; }
The follow error is thrown when I go to the URL that the controller is mapped to.
Fatal error: Uncaught TypeError: Argument 1 passed to Tester\ExamplePaymentGateway\Controller\Example\AbstractExample\Start::__construct() must implement interface Magento\Framework\UrlInterface, instance of Magento\Framework\App\Action\Context given, called in var/generation/Tester/ExamplePaymentGateway/Controller/Example/Start/Interceptor.php on line 14 and defined in app/code/Tester/ExamplePaymentGateway/Controller/Example/AbstractExample/Start.php:24
Stack trace:
#0 var/generation/Tester/ExamplePaymentGateway/Controller/Example/Start/Interceptor.php(14):
Tester\ExamplePaymentGateway\Controller\Example\AbstractExample\Start->__construct(Object(Magento\Framework\App\Action\Context), Object(Magento\Customer\Model\Session\Interceptor), Object(Magento\Checkout\Model\Session\Interceptor), Object(Magento\Sales\Model\OrderFactory), Object(Magento\Paypal\Model\Express\Checkout\Factory), Object(Magento\Framework\Session\Ge in app/code/Tester/ExamplePaymentGateway/Controller/Example/AbstractExample/Start.php on line 24
Am I missing some step before I can inject the URL builder or any other objects (let's say quote, cart, or payment helper)?
2 Answers 2
If your controller extend Magento\Framework\App\Action\Action you should follow rules of creation of the controller. So you should implement dependency injection of the parent class constructor (Magento\Framework\App\Action\Action):
/**
* @param Context $context
*/
public function __construct(Context $context)
{
parent::__construct($context);
$this->_objectManager = $context->getObjectManager();
$this->_eventManager = $context->getEventManager();
$this->_url = $context->getUrl();
$this->_actionFlag = $context->getActionFlag();
$this->_redirect = $context->getRedirect();
$this->_view = $context->getView();
$this->messageManager = $context->getMessageManager();
}
Your construct method should look like this after a modifications:
/**
* @param Context $context
* @param UrlInterface $urlBuilder
*/
public function __construct(
Context $context,
UrlInterface $urlBuilder
) {
parent::__construct($context);
$this->urlBuilder = $urlBuilder;
}
NOTE: do not forget to remove old var/generation directory after implementing of modifications, because there are stored the generated files of controllers.
-
Thank you for the reply.. I tried to add the construct method on my controller, but got the following error: Fatal error: Uncaught TypeError: Argument 1 passed to Tester\ExamplePaymentGateway\Controller\Example\AbstractExample\Start::__construct() must be an instance of Tester\ExamplePaymentGateway\Controller\Example\AbstractExample\Context, instance of Magento\Framework\App\Action\Context given, called in /home/magento_user/public_html/var/generation/Tester/ExamplePaymentGateway/Controller/Example/Start/Interceptor.php on line 14frostshoxx– frostshoxx2016年09月03日 12:55:15 +00:00Commented Sep 3, 2016 at 12:55
-
@frostshoxx Most likely you created the class of a context and added it in the abstract controller. It is necessary to see a code completely.Siarhey Uchukhlebau– Siarhey Uchukhlebau2016年09月03日 12:59:25 +00:00Commented Sep 3, 2016 at 12:59
-
The issue was me not creating an abstract controller. Thank you for your help.frostshoxx– frostshoxx2016年09月06日 16:50:58 +00:00Commented Sep 6, 2016 at 16:50
actually from controller you can use
$this->_url->getUrl('yourmoduleurl/index/index')
Explore related questions
See similar questions with these tags.
var/generation?