0

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>
2
  • Please update your etc/frontend/routes.xml file's content in your question. Commented Dec 13, 2019 at 5:43
  • Not anything you're missing here. Can you please update your question with file path ? May be something wrong is there. Commented Dec 13, 2019 at 5:50

2 Answers 2

4

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 :

enter image description here

Hope this will help you!

1

Calling wrong route. call

 hello_test_displayInfo

URL :

 hello/test/displayInfo
3
  • @Akash Check , my asnwer is it works? Commented Dec 13, 2019 at 6:01
  • seems like doesn't matter. Its running with both names.Problem was location of controller. Commented Dec 13, 2019 at 7:07
  • @AkashKilledar ok, Thanks Good luck Commented Dec 13, 2019 at 7:08

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.