404 page not found error. Custom Page
Question
I am trying to create custom page page having error 404.
routes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. 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="hello" frontName="hello">
<module name="SimpleMagento_Custom" />
</route>
</router>
</config>
Controller :-
<?php
namespace SimpleMagento\Custom\Controller\Test;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class DisplayInfo extends Action
{
protected $_pagefactory;
public function __construct(PageFactory $pagefactory,Context $context)
{
$this->_pagefactory=$pagefactory;
parent::__construct($context);
}
public function execute()
{
return $this->_pagefactory->create();
}
}
?>
Layout file (hello_test_displayinfo):-
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="SimpleMagento\Custom\Block\EmployeeData" name="new_layout1" template="SimpleMagento_Custom::Employee.phtml"/>
</referenceContainer>
</body>
</page>
Block file:-
<?php
namespace SimpleMagento\Custom\Block;
class EmployeeData extends \Magento\Framework\View\Element\Template
{
public function __construct(\Magento\Framework\View\Element\Template\Context $context)
{
parent::__construct($context);
}
public function getName()
{
echo "YES..!";
}
}
?>
Employee.phtml:-
<h1>you have created new page </h1>
<h3><?php $block->getName();?></h3>
Solution
Please correct your file path with below structure
app/code/SimpleMagento/Custom/registration.php
Content for this file is..
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'SimpleMagento_Custom',
__DIR__
);
app/code/SimpleMagento/Custom/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="SimpleMagento_Custom" setup_version="1.0.0" />
</config>
app/code/SimpleMagento/Custom/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="hello" frontName="hello">
<module name="SimpleMagento_Custom" />
</route>
</router>
</config>
app/code/SimpleMagento/Custom/Controller/Test/DisplayInfo.php
<?php
namespace SimpleMagento\Custom\Controller\Test;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class DisplayInfo extends Action
{
protected $_pagefactory;
public function __construct(PageFactory $pagefactory,Context $context)
{
$this->_pagefactory=$pagefactory;
parent::__construct($context);
}
public function execute()
{
return $this->_pagefactory->create();
}
}
?>
app/code/SimpleMagento/Custom/Block/EmployeeData.php
<?php
namespace SimpleMagento\Custom\Block;
class EmployeeData extends \Magento\Framework\View\Element\Template
{
public function __construct(\Magento\Framework\View\Element\Template\Context $context)
{
parent::__construct($context);
}
public function getName()
{
echo "YES..!";
}
}
?>
app/code/SimpleMagento/Custom/view/frontend/layout/hello_test_displayinfo.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="SimpleMagento\Custom\Block\EmployeeData" name="new_layout1" template="SimpleMagento_Custom::Employee.phtml"/>
</referenceContainer>
</body>
</page>
app/code/SimpleMagento/Custom/view/frontend/templates/Employee.phtml
<h1>you have created new page </h1>
<h3><?php $block->getName();?></h3>
URL :
http://www.example.com/hello/test/displayinfo
Output :
Hope this will help you!
OTHER TIPS
Calling wrong route. call
hello_test_displayInfo
URL :
hello/test/displayInfo