Magento – Magento 2 – Got Error in Adminhtml Controller

admin-controllermagento2module

I am trying to create custom module in magento2.
I got below error while click on the menu in admin.
it seems my admin controller not configured properly.

Can anyone help me in this issue.

Fatal error: Class [NameSpace][ModuleName]\Controller\Adminhtml[ModuleName]\Index contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Magento\Framework\App\ActionInterface::execute)

My controller file is containing below codes.

namespace [NameSpace]\[ModuleName]\Controller\Adminhtml\[ModuleName];
use Magento\Framework\Controller\ResultFactory;
class Index extends \[NameSpace]\[ModuleName]\Controller\Adminhtml\[ModuleName]
{   
    public function executeInternal()
    {   die('3243');
        $resultPage = $this->_initAction();
        $resultPage->getConfig()->getTitle()->prepend(__('Priya'));
        return $resultPage;
    }
}

where i have change in code.

Updated Question:
[NameSpace]/[ModuleName]/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="hello" frontName="hello">
        <module name="[NameSpace]_[ModuleName]" before="Magento_Backend" />
    </route>
</router>
</config>

[NameSpace]/[ModuleName]/etc/adminhtml/menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
    <add id="[NameSpace]_[ModuleName]::main_menu" title="Priya" module="[NameSpace]_[ModuleName]" sortOrder="80" action="hello/hello" resource="[NameSpace]_[ModuleName]::hello"/>
</menu>
</config>

[NameSpace]/[ModuleName]/Controller/Adminhtml/[ModuleName].php

<?php
namespace [NameSpace]\[ModuleName]\Controller\Adminhtml;
abstract class Hello extends \Magento\Backend\App\AbstractAction
{
protected $_coreRegistry;
public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\Registry $coreRegistry,
     \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    \Magento\Framework\Translate\InlineInterface $translateInline,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
    \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
) {parent::__construct($context);
    $this->_coreRegistry = $coreRegistry;
    $this->_coreRegistry = $coreRegistry;
    $this->_fileFactory = $fileFactory;
    $this->_translateInline = $translateInline;
    $this->resultPageFactory = $resultPageFactory;
    $this->resultJsonFactory = $resultJsonFactory;
    $this->resultLayoutFactory = $resultLayoutFactory;
    $this->resultRawFactory = $resultRawFactory;
}
protected function _initAction()
{
    $resultPage = $this->resultPageFactory->create();

    return $resultPage;
}
protected function _isAllowed()
{
    return $this->_authorization->isAllowed('[NameSpace]_[ModuleName]::hello');
}

}

[NameSpace]/[ModuleName]/Controller/Adminhtml/[ModuleName]/Index.php

<?php
namespace [NameSpace]\[ModuleName]\Controller\Adminhtml\[ModuleName];
use Magento\Framework\Controller\ResultFactory;
class Index extends [NameSpace]\[ModuleName]\Adminhtml\[ModuleName] {   

protected $_publicActions = ['index']; 
protected $resultPageFactory;

public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
 }

public function execute()
{  
   echo "Hello World";
}

}

Best Answer

Folder structure for following controller will be app/code/Test/Priya/Controller/Adminhtml/Priya in side that folder Index.php

namespace Test\Priya\Controller\Adminhtml\Priya;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

 class Index extends \Magento\Backend\App\Action
 {   
       protected $_publicActions = ['index']; 
       protected $resultPageFactory;

        /**
         * @param Context $context
         * @param PageFactory $resultPageFactory
         */
           public function __construct(
            Context $context,
            PageFactory $resultPageFactory
            ) {
            parent::__construct($context);
            $this->resultPageFactory = $resultPageFactory;
             }

            public function execute()
            {  
               echo "Hello World";
            }
}

You can hit directly controller at backend without key to test your admin controller.

priya/priya/index

To enable developer mode you can do it from .htaccess at root of your magento2 directory. Remove hash from below code.

#   SetEnv MAGE_MODE developer

app\code\Test\Priya\etc\adminhtml\routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="admin">
        <route id="priya" frontName="priya">
            <module name="Test_Priya" before="Magento_Backend" />
        </route>
    </router>
</config>