1

I don't understand why but in many topics I see that to get and admin route url with secure key and parameter I only need to use :

$this->urlBuilder->getUrl('adminhtml/myroutename/mycontroller/myaction', array('myparam' => 'myvalue'))

and urlBuilder I a Magento\Framework\UrlInterface object.

But in front, I get this URL: http://mysite.lan/adminhtml/myroutename/mycontroller/myparam/myvalue/

And if I remove the adminhtml I get this URL: http://mysite.lan/myroutename/mycontroller/myaction/myparam/myvalue/

So how can I create a correct admin URL with the key and the params?

1
  • Where you use $this->urlBuilder(), In frontend or admin? Commented Oct 16, 2017 at 13:22

2 Answers 2

1

If you are in an admin controller, you can just get the URL from the context (which is injected in the constructor):

$url = $this->context->getUrl();

$myUrl = $url->getUrl('adminhtml/myroutename/mycontroller/myaction', array('myparam' => 'myvalue'));

/This change with remove bracket/

0
5

There is a backend URL builder that backend controllers utilize.
Excerpt from abstract backend controller:

<?php

namespace Magento\Backend\App;

abstract class AbstractAction extends \Magento\Framework\App\Action\Action
{
    /**
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(Action\Context $context)
    {
        parent::__construct($context);
        // ...
        $this->_backendUrl = $context->getBackendUrl();
        // ...
    }

// ...

You can access the URL builder from within any controller via $this->_backendUrl or inject it directly into a class:

<?php

use Magento\Backend\Model\UrlInterface;

class ExampleClass
{
    public function __construct(UrlInterface $backendUrl) {
        $this->backendUrl = $backendUrl;
    }
}

From there, you can build admin URL's like so:

$this->_backendUrl->getUrl(
    'myroutename/mycontroller/myaction',
    [
        'myparam1' => 'myvalue1',
        'myparam2' => 'myvalue2'
    ]
);

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.