2

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";
}

}

  • Are you trying to create admin controller ? then only execute() method should be there. Why you are using executeInternal? and it should extends \Magento\Backend\App\Action – Bhupendra Jadeja Jul 13 '16 at 6:29
  • @BhupendraJadeja yes i am creating admin controller..ok i will use execute method and let you know, since m new to magento2 – Priya Ponnusamy Jul 13 '16 at 6:38
  • ok ! happy it works for you – Bhupendra Jadeja Jul 13 '16 at 6:42
  • using execute() is working but it didnot show my admin it displays "There has been an error processing your request" – Priya Ponnusamy Jul 13 '16 at 6:42
4

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>
  • while using this code it gives error report – Priya Ponnusamy Jul 13 '16 at 6:51
  • Hope you are creating directory structure according to your module !! and you have created Test\Priya\etc\adminhtml\routes.xml where test is your vendor name and priya is your module name – Bhupendra Jadeja Jul 13 '16 at 6:54
  • yes i have that – Priya Ponnusamy Jul 13 '16 at 6:58
  • kindly check my updated question – Priya Ponnusamy Jul 13 '16 at 6:59
  • I have updated my answer please check – Bhupendra Jadeja Jul 13 '16 at 7:01
3

You have to use the execute() method inside the controller. Each controller file has a single execute method. This execute method is responsible for returning a “result” object (i.e. an object that implements the Magento\Framework\Controller\ResultInterface interface).

See the code below :

namespace [NameSpace]\[ModuleName]\Controller\Adminhtml\[ModuleName];
use Magento\Framework\Controller\ResultFactory;
class Index extends \[NameSpace]\[ModuleName]\Controller\Adminhtml\[ModuleName]
{   
    public function execute()
    {   die('3243');
        $resultPage = $this->_initAction();
        $resultPage->getConfig()->getTitle()->prepend(__('Priya'));
        return $resultPage;
    }
}
  • using execute() is working but it didnot show my admin it displays "There has been an error processing your request" – Priya Ponnusamy Jul 13 '16 at 6:42
  • turn on the developer mode to know the error – Manashvi Birla Jul 13 '16 at 6:46
  • yes i did...there i get below arror Recoverable Error: Argument 1 passed to [NameSpace][ModuleName]\Controller\Adminhtml[ModuleName]\Index::__construct() must be an instance of [NameSpace][ModuleName]\Controller\Adminhtml[ModuleName]\Context, instance of Magento\Backend\App\Action\Context given, called in C:\wamp\www\magento-2.0.7\var\generation[NameSpace][ModuleName]\Controller\Adminhtml[ModuleName]\Index\Interceptor.php on line 14 and defined in C:\wamp\www\magento-2.0.7\app\code[NameSpace][ModuleName]\Controller\Adminhtml[ModuleName]\Index.php on line – Priya Ponnusamy Jul 13 '16 at 7:05
  • post the full code of your controller along with the constructor – Manashvi Birla Jul 13 '16 at 7:08
  • Ok i will update now – Priya Ponnusamy Jul 13 '16 at 7:09

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.