cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.4 custom page doesn't work [URGENT]

SOLVED

Magento 2.4 custom page doesn't work [URGENT]

Dear Magento Community,

 

I've tried to create a simple custom page, tried out many tutorials, and read a lot of solutions on forums, but none of them work for me. My module can be enabled but I keep getting redirected when I go to the URL path I've created. Here is the content of my files (with their paths) so can someone please check it out, I need the solution urgently

 

app/code/ZenDev/HelloZenDev/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Hello_ZenDev',
    __DIR__
);

 

app/code/ZenDev/HelloZenDev/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="Hello_ZenDev" setup_version="0.0.1"></module>
</config>
 

 

app/code/ZenDev/HelloZenDev/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="zendev_hellozendev" frontName="zendev">
<module name="Hello_ZenDev" />
</route>
</router>
</config>

 

app/code/ZenDev/HelloZenDev/Controller/Page/View.php

<?php

declare(strict_types=1);

namespace ZenDev\HelloZenDev\Controller\Page;

use Magento\Framework\Controller\Result\Json;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;

class View extends Action
{
    public function execute()
    {
        /** @var Json $jsonResult */
        $jsonResult = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $jsonResult->setData([
            'message' => 'Hello ZenDev!'
        ]);
        return $jsonResult;
    }
}

I go to the page http://localhost:8080/zendev/page/view and I'm redirected to 404 page (keep in mind I've setup my magento page to be on localhost:8080 and installation worked fine)

 

Thankful in advance 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2.4 custom page doesn't work [URGENT]

Hello @berinabandede1 

 

The issue is because the directory name and module name is mismatched.
 
Your Folder structure should be like app/code/Hello/ZenDev
 
And, controller file namespace should be like namespace Hello\ZenDev\Controller\Page;
 
URL should be localhost/storename/router/controller like http://localhost:8080/magento24/zendev/page/view instead of http://localhost:8080/zendev/page/view
 
I hope it helps.
 
Thank you.
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

View solution in original post

2 REPLIES 2

Re: Magento 2.4 custom page doesn't work [URGENT]

Hello @berinabandede1 

 

The issue is because the directory name and module name is mismatched.
 
Your Folder structure should be like app/code/Hello/ZenDev
 
And, controller file namespace should be like namespace Hello\ZenDev\Controller\Page;
 
URL should be localhost/storename/router/controller like http://localhost:8080/magento24/zendev/page/view instead of http://localhost:8080/zendev/page/view
 
I hope it helps.
 
Thank you.
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Magento 2.4 custom page doesn't work [URGENT]

Hi @Sanjay Jethva 

 

My URL was correct because that's the only store I have, but the problem was in the naming of my folders as you've mentioned above 

 

Thank you so much!