0

I'm trying to figure out why my custom controller is redirecting to the sales dashboard.

The way I understand it when Magento has an issue with either the form key or the routes, it just redirects to the default page (in this case the admin sales dashboard). It seems that I am having an issue as this happens for one of my controllers.

I should mention that there is no error as well, normally I get "invalid security key" or something like that but in this case it had none.

/Controller/Adminhtml/Test/Index.php

namespace Companyname\TestModule\Controller\Adminhtml\Test;

class Index extends \Magento\Backend\App\Action
{
    protected $_publicActions = ['index'];
    
    
    public function __construct
    (
       \Magento\Backend\App\Action\Context $context
    )
    {
       parent::__construct($context);
    }
     
    public function execute()
    {
        die("It works.");
    }
    
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Companyname_TestModule::test');
    }
}

/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="test" frontName="test">
            <module name="Companyname_TestModule"/>
        </route>
    </router>
</config>

URL function:

public function getURL()
{
    $route = "test/test/index";
    $params = [];
        
    return $this->getUrl($route, $params);
}
5
  • Please post your acl.xml and also menu.xml (if using one) and/or the code where you generate the URL that points to your custom controller. Commented Jul 13, 2021 at 3:38
  • Adding the url method now, I'm not using an acl or menu xml as this page is intended as an action page that redirects when done rather than one that needs to output content when viewed.
    – joeybab3
    Commented Jul 13, 2021 at 16:58
  • It sounds like you need an acl.xml because you're checking Companyname_TestModule::test in the _isAllowed() method. Try returning true from isAllowed() just to see if that fixes your issue Commented Jul 14, 2021 at 12:57
  • Unfortunately returning true or just omitting the method don't solve the issue.
    – joeybab3
    Commented Jul 14, 2021 at 15:35
  • I wasn't suggesting that as the fix. I was saying to do that to determine if that is the problem. You are specifying a resource $this->_authorization->isAllowed('Companyname_TestModule::test') but you haven't defined that resource in acl.xml. Setting isAllowed to true will help you narrow down the source of the problem. Commented Jul 14, 2021 at 20:12

3 Answers 3

0

In /etc/adminhtml/routes.xml

<module name="Companyname_Test"/>

There need to be "Companyname_TestModule. Your controller's namespace is Companyname\TestModule\Controller\Adminhtml\Test, so the module name is Companyname_TestModule.

1
  • Thanks for catching that, unfortunately, it does not fix the issue and was just an artifact from prepping my code to post here.
    – joeybab3
    Commented Jul 13, 2021 at 20:20
0

In this case, I would debug by using these steps:

  1. Carefully double-check my PHP namespace and folders to ensure there are no typos.
  2. Open a core module and compare to make sure all of my configuration matches appropriately – even folders. Structure should match while names should be different.
  3. Double check my configuration names to make sure they are what they should be.
  4. Make sure that the URL creation class import is the one for admin URLs (\Magento\Backend\Model\UrlInterface), not the regular, front end one.
  5. Compare the actual URL in the browser with one from a core module. Here, make sure it includes the admin URL as well: /admin/test/test/index.
  6. Open DevTools with Network -> Preserve Log checked and click the link to see what kind of HTTP response I get initially. Look carefully at the Location header and request to see if there HTTP(s) problems that I can notice there.
  7. Finally, the ultimate, but more time intensive: use xdebug within Magento's routing system to determine why it's redirecting instead of allowing you to that route.
3
  • For point #4 I'm using the built in $this->getUrl() of the parent \Magento\Framework\View\Element\Template that I'm extending.
    – joeybab3
    Commented Jul 14, 2021 at 15:37
  • @joeybab3, why is that? Why not the backend one? Commented Jul 14, 2021 at 16:54
  • Not sure, that's just how I built it. But even using the URL interface you specified does not solve the issue, unfortunately.
    – joeybab3
    Commented Jul 14, 2021 at 17:04
0

If your controller extends Magento\Framework\App\Action\Action you should follow the rules of creation of the controller. So you should implement dependency injection of the parent class constructor (Magento\Framework\App\Action\Action):

Your construct method should look like this after a modifications:

public function __construct(
    Context $context,
    UrlInterface $urlBuilder
) {
    parent::__construct($context);
    $this->urlBuilder = $urlBuilder;
}
2
  • So you're saying I'm missing the UrlInterface?
    – joeybab3
    Commented Jul 20, 2021 at 15:37
  • That's right, in your case it would be: $route = $this->urlBuilder->getUrl('test/test/index') Commented Jul 22, 2021 at 2:05

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.