1

I know that there are similar questions to this but none of the other questions fix my problem.

I'm still doing Magento training and I'm practicing to create a frontend controller that renders "HELLO WORLD" (module name: Unit2_HelloWorldController)

What I have created:

  1. Declare a route in Unit2/HelloWorldController/etc/frontend/routes.xml
<?xml version="1.0"?> 
<!-- 
    /**  
    * Copyright © Magento. All rights reserved. 
    * See COPYING.txt for license details. 
    */ 
-->   
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">    
    <router id="standard">        
        <route id="helloworldcontroller" frontName="helloworldcontroller">            
            <module name="Unit2_HelloWorldController" />         
        </route>    
    </router>
</config>
  1. Create an action class in HelloWorldController/Controller/index.php
<?php
/** 
* 
* Copyright © Magento. All rights reserved. 
* See COPYING.txt for license details. 
*/
namespace Unit2\HelloWorldController\Controller\Action;
/** 
* Class Index 
* @package Unit2\HelloWorldController\Controller\Action 
*/
class Index extends \Magento\Framework\App\Action\Action {
    /**     
    * @var \Magento\Framework\View\Result\PageFactory     
    */
    protected $_pageFactory;
    /**     
    * Index constructor.     
    * @param \Magento\Framework\App\Action\Context $context    
    * @param \Magento\Framework\View\Result\PageFactory $pageFactory     
    */
    public function __construct(        
        \Magento\Framework\App\Action\Context $context,        
        \Magento\Framework\View\Result\PageFactory $pageFactory    
    )     
    {         
        $this->_pageFactory = $pageFactory;        
        return parent::__construct($context);     
    } 
    /**     
    * @return \Magento\Framework\Controller\ResultInterface     
    */public function execute()    
    {         
        $result = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_RAW);     
        $result->setContents('Hello World');        
        return $result;     
    } 
}
  1. Create HelloWorldController/registration.php
<?php
/** 
* 
* Copyright © Magento. All rights reserved. * See COPYING.txt for license details. 
*/
\Magento\Framework\Component\ComponentRegistrar::register(     
    \Magento\Framework\Component\ComponentRegistrar::MODULE, 
    'Unit2_HelloWorldController', 
    __DIR__
);
  1. Create HelloWorldController/etc/module.xml
<?xml version="1.0"?>
<!--/** * * Copyright © Magento. All rights reserved. * See COPYING.txt for license details. */--><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">    
    <module name="Unit2_HelloWorldController" setup_version="0.0.1"></module>
</config>

Then I ran these commands

php bin/magento setup:upgrade 
php bin/magento setup:static-content:deploy -f

and when I try to open http://localhost:8080/magento2/helloworldcontroller/action/index I got 404 Not Found.

What am I doing wrong here? Any helps would be appreciated. Thank you.

6
  • Welcome @student001, Follow this link github.com/mageplaza/magento-2-hello-world Jun 18, 2020 at 19:45
  • I followed the step, but still get 404 Not Found page @MohitPatel
    – student001
    Jun 18, 2020 at 20:14
  • working fine for me check --- prnt.sc/t2b6nm . And Please share your controller path Jun 18, 2020 at 20:43
  • sorry, I'm still so new to Magento. What do you mean by controller path? @MohitPatel. If I understand correctly, it is app/code/Unit2/HelloWorldController. Am I missing any steps?
    – student001
    Jun 19, 2020 at 18:55
  • i will add my full code and you review code if you not understand let me know Jun 19, 2020 at 18:58

1 Answer 1

1

Firstly, Create app/code/Mageplaza/HelloWorld/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mageplaza_HelloWorld',
    __DIR__
); 

app/code/Mageplaza/HelloWorld/etc/module.xml

<?xml version="1.0"?>
<config
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Mageplaza_HelloWorld" setup_version="1.0.0" />
</config>

app/code/Mageplaza/HelloWorld/etc/frontend/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="mageplaza" frontName="helloworld">
            <module name="Mageplaza_HelloWorld" />
        </route>
    </router>
</config>

app/code/Mageplaza/HelloWorld/Controller/Index/Display.php

<?php
namespace Mageplaza\HelloWorld\Controller\Index;

class Display extends \Magento\Framework\App\Action\Action
{
  public function __construct(
\Magento\Framework\App\Action\Context $context)
  {
    return parent::__construct($context);
  }

  public function execute()
  {
    echo 'Hello World';
    exit;
  }
}

Your code is ready After

remove your var/cache folder
remove your var/view_preprocessed folder
php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
php bin/magento c:f

After you open the browser and run http://your_url.com/helloworld/index/display

Hope this help you

Thanks ...

4
  • Really usefull answer , upvoted this. Jun 19, 2020 at 19:30
  • Welcome,many thanks for your kind words @HamendraSunthwal ... Jun 19, 2020 at 19:46
  • Thank you @MohitPatel. I have a question, when you create a new module, do you always have to remove cache and view_preprocessed folders?
    – student001
    Jun 19, 2020 at 20:25
  • on my word custom module unnecessary code paste in folder.so we remove... Jun 19, 2020 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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