0

I am new to Magento and I tried this tutorial where I will create a controller for Magento 2. https://www.mageplaza.com/magento-2-module-development/how-to-create-controllers-magento-2.html

My problem is: When I try to go to the http://<yourbrowser>/helloworld or http://<yourbrowser>/helloworld/index/index, it renders a blank page.

I followed everything from the tutorial, just I renamed the /vendorName/moduleName to /custom/helloWorld

Here are the codes: app/code/custom/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 frontName="helloworld" id="helloworld">
            <module name="custom_helloWorld"/>
        </route>
    </router>
</config>

app/code/custom/helloWorld/Controller/Index/Index.php

<?php
namespace custom\helloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
        //Pagefactory is used to initialize the layout.
    }
}

app/code/custom/helloWorld/view/frontend/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="custom\helloWorld\Block\Index" name="helloworld_index_index" template="custom_helloWorld::index.phtml" />
    </referenceContainer>
</page>

app/code/custom/helloWorld/Block/Index.php

<?php
namespace custom\helloWorld\Block;
class Index extends \Magento\Framework\View\Element\Template
{

}

app/code/custom/helloWorld/view/frontend/templates/index.phtml

<h2>Welcome to HELLO WORLD PAGE!</h2>

I already tried to flush the cache but same problem occur.

I hope you can help me. Thank you so much in advance!!


UPDATE: So I just copied whatever is in the link: https://www.mageplaza.com/magento-2-module-development/how-to-create-controllers-magento-2.html

And still renders a blank page...

1
  • create module after you run php bin/magento s:up && php bin/magento s:s:d -f ??? – Mohit Patel Jun 19 '20 at 4:57
0

As I can see you start vendor name and module name with small character. Ex: "app/code/custom/helloWorld/Block/Index.php"

But it should be like "app/code/Custom/HelloWorld/Block/Index.php"

Kindly refer below link for create custom module:

https://www.mageplaza.com/magento-2-module-development/how-to-create-controllers-magento-2.html

1
  • I changed it to "app/code/Custom/HelloWorld/Block/Index.php". And correct the paths on Block, Controller, etc, view folders. As well as the registration.php. Result when I try to access http://<yourbrowser>/helloworld: 404 Not Found Error. – RainDV Jun 22 '20 at 7:24
0

Update Your helloworld_index_index.xml with this ,

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="helloworld_index_index" template="custom_helloWorld::index.phtml" />
    </referenceContainer>
</page>

No need of block file... flush the Cache and Run in Browser it works fine...

4
  • Thank you Kishor. I tried, but 404 Error not Found occurs when I run in browser :( – RainDV Jun 23 '20 at 1:39
  • may be you doing some wrong with the syntax ...have you run command setup:upgrade? – Kishor 6ugani Jun 23 '20 at 12:31
  • yes, I did run the command setup:upgrade. Still Blank Page. – RainDV Jun 24 '20 at 6:26
  • have your magento work fine? Although Follow this blog link...Its work fine – Kishor 6ugani Jun 24 '20 at 7:01
0

First rename your module to Custom/HelloWorld - Module name and name space should be having first letter capital ..Its good practise.

Than change code like below

app/code/Custom/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 frontName="helloworld" id="helloworld">
            <module name="Custom_HelloWorld"/>
        </route>
    </router>
</config>

app/code/Custom/HelloWorld/Controller/Index/Index.php

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

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
        //Pagefactory is used to initialize the layout.
    }
}

app/code/Custom/HelloWorld/view/frontend/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="content">
        <block class="Custom\HelloWorld\Block\Index" name="helloworld_index_index" template="Custom_HelloWorld::index.phtml" />

    </referenceContainer>
</page>

app/code/Custom/HelloWorld/Block/Index.php

<?php
namespace Mageplaza\HelloWorld\Block;
class Custom extends \Magento\Framework\View\Element\Template
{

}

app/code/Custom/HelloWorld/view/frontend/templates/index.phtml

<h2>Welcome to HELLO WORLD PAGE!</h2>
2
  • Thank you Parul. Copied all your code, except, I corrected the app/code/Custom/HelloWorld/Block/Index.php namespace and class, I flushed the Magento Cache, and access the /helloworld url. Still 404 Not Found Error occurs. – RainDV Jun 23 '20 at 1:35
  • Do run command php bin/magento setup:di:compile and let me know if any problem occurs – ParulThakkar Jun 23 '20 at 5:48

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.