1

I created a custom module in adminportal with custom controller in magento 2 but the controller action is giving 404 error page can someone help me on this?

mymodule folder structure is

app/code/Gokulpriya/CustomMenu/Controller/Adminhtml/Index/Index.php

 <?php
namespace Gokulpriya\CustomMenu\Controller\Adminhtml\Index;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\ResultFactory;
class Index extends Action
{

    public function execute()
    {
        $result = $this->resultFactory->create(ResultFactory::TYPE_RAW);

        $result->setContents('Hello world');

        return $result;
    }
}
  • Share all the files of your module. – Anshu Mishra Feb 27 at 12:50
  • Please check your class name.Your controller name is Test but you are using Index instead.. – Ghulam.M Feb 27 at 13:30
  • Yeah that is one the mistake and the other mistake is i missed composer.json file while creating a module,Now it got rectified thanks for the help and support. – Gokulpriya Feb 28 at 10:49
0

Please check below files and module structure for sample admin module

app\code\Mycompany\Mymodule\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Mycompany_Mymodule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>

app\code\Mycompany\Mymodule\etc\adminhtml\menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/menu.xsd">
    <menu>
        <add id="Mycompany_Mymodule::parent" title="Mymodule" module="Mycompany_Mymodule" sortOrder="100" resource="Mycompany_Mymodule::parent"/>
        <add id="Mycompany_Mymodule::index" title="Mymodule Index" module="Mycompany_Mymodule" sortOrder="10" action="mymodule/index" resource="Mycompany_Mymodule::index" parent="Mycompany_Mymodule::parent"/>
    </menu>
</config>

app\code\Mycompany\Mymodule\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="mymodule" frontName="mymodule">
            <module name="Mycompany_Mymodule" before="Magento_Adminhtml" />
        </route>
    </router>
</config>

app\code\Mycompany\Mymodule\Controller\Adminhtml\Index\Index.php

<?php
namespace Mycompany\Mymodule\Controller\Adminhtml\Index;
class Index extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}
?>

app\code\Mycompany\Mymodule\Block\Adminhtml\Index\Index.php

<?php
namespace Mycompany\Mymodule\Block\Adminhtml\Index;
class Index extends \Magento\Backend\Block\Widget\Container
{
    public function __construct(\Magento\Backend\Block\Widget\Context $context,array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function getHellowword()
    {
        return 'Demo Hellowword';
    }
}
?>

app\code\Mycompany\Mymodule\view\adminhtml\layout\mymodule_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Mycompany Page Title</title>
    </head> 
    <body>
        <referenceContainer name="content">
            <block class="Mycompany\Mymodule\Block\Adminhtml\Index\Index" name="mymodule_block_adminhtml_index_index"  template="Mycompany_Mymodule::mymodule_index_index.phtml" />
        </referenceContainer>
    </body>
</page>

app\code\Mycompany\Mymodule\view\adminhtml\templates\mymodule_index_index.phtml

<?php 
echo $block->getHellowword();
?>

and don't forget to create composer.json and registration.php in your module's root

and you can access your module by adding mymodule/index/index after admin base URL.

  • Yeah @VDC followed your process of creating modules It works fine now...My mistake is I missed composer.json file . I've read many of the tutorials none of them speaks about composer.json file only now i got to know this..Thanks a lot. – Gokulpriya Feb 28 at 10:47
  • @Gokulpriya My Pleasure – VDC Feb 28 at 12:02
0

First create routes.xml file under

Vendor/Module/etc/adminhtml/routes.xml

<router id="admin">
    <route id="module" frontName="module">
        <module name="Vendor_Module"/>
    </route>
</router>


Now create controller file like:

Vendor/Module/Controller/Extension/Index

namespace Vendor\Module\Controller\Adminhtml\Extension;

class Index extends \Magento\Backend\App\Action
{

protected $resultPageFactory;



/**
   * Constructor
   *
   * @param \Magento\Backend\App\Action\Context $context
   * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
   */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
         parent::__construct($context);
         $this->resultPageFactory = $resultPageFactory;
    }

  /**
   * Load the page defined in view/adminhtml/layout/download_extension_index.xml
   *
   * @return \Magento\Framework\View\Result\Page
   */
    public function execute()
    {
         return  $resultPage = $this->resultPageFactory->create();
    }
}

Now create file like:

Vendor/Module/view/adminhtml/layout/vendor_module_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Adminhtml\CustomBlock" name="vendor_module_block" template="Vendor_Module::custom.phtml"/>
        </referenceContainer>
    </body>
</page>

Finally create phtml file:

Vendor/Module/view/adminhtml/templates/custom.phtml

<H5>Hello<h5>

This may help for you.

  • My mistake is i missed composer.json file while creating a module,Now it got rectified thanks for the help and support. – Gokulpriya Feb 28 at 10:50

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.