6

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.

  1. Create a functional controller and set up route. It is accessible via web browser and has execute() function defined.
  2. Add use Magento\Framework\UrlInterface; in a controller class
  3. Add a protected variable $urlBuilder
  4. 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)?

1
  • Tried to remove var/generation? Commented Sep 3, 2016 at 12:16

2 Answers 2

7

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.

3
  • 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 14
    – frostshoxx
    Commented 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. Commented Sep 3, 2016 at 12:59
  • The issue was me not creating an abstract controller. Thank you for your help.
    – frostshoxx
    Commented Sep 6, 2016 at 16:50
13

actually from controller you can use

$this->_url->getUrl('yourmoduleurl/index/index')

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.