0

I am trying to create a new controller for a module. I have set the frontend layout XML file and corresponding template file. I have flushed the cache too. Still it's showing a 404 error. What am I doing wrong here? If I need to mention more information for answers, please do mention.

<?php

namespace Mine\Hello\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;


class Googi extends Action
{
    /**
     * @var PageFactory
     */
    protected $_resultPageFactory;

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

    /**
     * Hello Landing page.
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $resultPage = $this->_resultPageFactory->create();
        // Set title of page
        $resultPage->getConfig()->getTitle()->set(__('Something'));
        return $resultPage;
    }
}

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="standard">
        <route id="hello" frontName="hello">
            <module name="Mine_Hello" />
        </route>
    </router>
</config>

The URL I am specifying is Magento/hello/index/googi.

  • 1
    Please put the content of routes.xml file and your controller file. – Mohit Kumar Arora Jan 12 '18 at 12:36
3

To create a new controller you first you need to create routes.xml in below location,

app\code\YOUR_VENDORNAME\YOUR_MODULE_NAME\etc\frontend\routes.xml

In this file, you need to write this code

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="SOME_UNIQUQ_ID" frontName="mytest">
            <module name="YOURVENDORNAME_YOURMODULENAME" />
        </route>
    </router>
</config>

Where mytest is my route name you can replace with your

Now you need to create a layout file in below file,

app\code\YOUR_VENDORNAME\YOUR_MODULE_NAME\view\frontend\layout\mytest_index_index.xml

here you can see mytest_index_index where mytest is route name index is controller name and index is action name.

in this layout file you need to write your as per your requirement. for example

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>TEST TITLE</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="YOUR_VENDOR\YOUR_MODULENAME\Block\MyBLock" name="myForm" template="YOURVENDOR_YOURMODULE::form.phtml" />            
        </referenceContainer>
    </body>
</page>

Now you need to create a block YOUR_VENDOR\YOUR_MODULENAME\Block\MyBLock and you can add your content

Now you need to create a controller in

app\code\YOUR_VENDORNAME\YOUR_MODULE_NAME\Controller\Index.php

And last but not the least execute below command,

  • php bin/magento setup:di:compile
  • php bin/magento c:f
1

Following step may be help you step

  • php bin/magento setup:di:compile
  • php bin/magento c:f
  • Check route front name from vendor/module/etc/frontend/route.xml file.
  • Make sure your controller action file inside the vendor/module/Controller/ControllerNameFolder/ActionNameFile.
  • Then hit BaseUrl/frontname/controller/action
  • If still having issue , debug controller by adding exit and die function in execute function . To check whether controller is working or not .
  • have tried that, i have put more info in the question, please refer. – Midlaj Jan 12 '18 at 13:01
0

You can check thru this command is your module is enabled. Seems that it's not yet enabled, you can check this through:

php bin/magento module:status

You can confirm that the module is not yet enabled if it's below "List of disabled modules:" You can enable it through:

php bin/magento module:enable Vendor_Module # <- replace this with your module

Afterwards, you can try

php bin/magento setup:upgrade

php bin/magento setup:di:compile

Then check again at your browser if you still encounter the 404 error.

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.