5

Here is my Directory structure of controller file in Magento 2 - app/code/Namespace/Modulename/Controller/Adminhtml/Blacklist/Index.php

In Index.php i write the below code -

namespace Mynamespace\Modulename\Controller\Adminhtml\ModuleDirectoryname;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;

class Index extends Action
{
    protected $helper;
    protected $context;

    public function __construct(
        Context $context,
        \VT\Blacklist\Helper\Data $helper
    ) {
        $this->viewHelper = $viewHelper;
        parent::__construct($context);
    }

    public function execute()
    {
        echo "hello"; die;
    }
}

I am using this Inject - \Magento\Backend\Model\UrlInterface $urlBuilder, in my another __construct function and printing url through -

echo $url = $this->urlBuilder->getUrl('namespace_modulename/adminhtml/moduledirectoryname/index');

but once i m running above printed url its redirecting to Dashboard not printing 'hello' ;

Can you plz let me know What I m missing here ?

  • How you browse this controller? – Sohel Rana Aug 31 '16 at 13:59
  • No need to use adminhtml in your path. Should be 'admin_route_frontName/moduledirectoryname/index'. admin_route_frontName - is from your etc/adminhtml/routes.xml – karick Sep 16 '17 at 8:55
5

I'm pretty sure it's because your URL is not properly generated.

Let's take the following example:

getUrl('adminRouteFrontName/controllerFolder/actionClass')

Here is how an URL should be generated and here is how you must replace the values:

  • adminRouteFrontName : it is the value declared in your adminhtml/routes.xml so I'm not sure what you put there in your case
  • controllerFolder : name of the controller folder under Controller/Adminhtml so in your case it's ModuleDirectoryname
  • actionClass : name of your action class under the controller folder for example with Index.php it would be index, for Grid.php it would be grid and so in your case it is index

So if you got those three things right, I'm pretty sure you should try removing the adminhtml from your parameter.

  • here 'namespace_modulename' is adminRouteFrontName and "adminhtml/moduledirectoryname/" is folder structure and "Index" is action class . – Atul Aug 31 '16 at 14:08
  • But If I create a directory in 'Adminhtml' directory and then difining the action class, So, Is it wrong way ? – Atul Aug 31 '16 at 14:10
  • 1
    @Atul no it's good but controllerFolder must be the name of the folder under Controller/Adminhtml so you need to remove adminhtml from your URL – Raphael at Digital Pianism Aug 31 '16 at 14:12
  • i tried its works but giving this issue - " Recoverable Error: Argument 1 passed to namespace\Modulename\Controller\Adminhtml\Blacklist\Index::__construct() must be an instance of Magento\Backend\App\Action\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in /home/mage2demo/public_html/mage21new/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /home/mage2demo/public_html/mage21new/app/code/Namespace\Modulename/Controller/Adminhtml/Blacklist/index.php on line 13 " – Atul Aug 31 '16 at 14:25
  • I also did upgrade n compilation but its coming after that also... – Atul Aug 31 '16 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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