2

I am trying to generate the following url, in the adminhtml area:

$this->url->getUrl('adminhtml/system_config/edit', ['section' => 'mysection']);
// where $this->url is an instance of \Magento\Framework\UrlInterface

and the url is being generated correctly.

The only thing is that another security key is being generated as part of the url (I'm getting "Invalid security or form key. Please refresh the page." on the next page after I click the generated link).

How can I preserve the current key?

2
  • 1
    If you are creating admin URL link, try using Magento\Backend\Model\UrlInterface class. Commented Jul 24, 2019 at 16:53
  • 1
    this solved my problem. you should add this comment as an answer and I'll be happy to approve it.
    – Radu
    Commented Jul 25, 2019 at 7:35

1 Answer 1

5

The issue with your approach is that you are using Magento\Framework\UrlInterface to create admin url. Magento has a separate Interface defined for Backend urls.

Please use Magento\Backend\Model\UrlInterface class to generate your URL with correct secret key.

1
  • 1
    Thanks for this! I would add that if you do not want the secret key part appended to the URL as I needed, you can then use the turnOffSecretKey() method Commented Mar 14, 2020 at 17:45

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.