0

I have created the new theme and new phtml file in magento2.1.5

The file path is app/design/frontend/demo/static/Magento_Theme/templates/html/staticpage.phtml

The staticpage.phtml file contains static content, I have used the URL http://localhost/magento2/staticpage.phtml but it shows the 404 page.

I want to show this file content in this URL, What are steps and changes needed to show phtml file content?

0

Well this is not a proper way, you should not call the file directly.

You will need a controller to handle your page and a layout in which you should call the phtml file.

if the controller already exist :

for ex : home page the controller will be module-cms/Controller/Index/Index.php and the layout will be cms_index_index.

So if you want to display the block on home page :

create following file in your theme : app/design/frontend/demo/static/Magento_Theme/layout/cms_index_index.xml

and write following code :

<?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">
    <body>
        <referenceContainer name="<block/container_in_which_you_want_to_show_your_block>">
            <block class="<the_block_class_you_want_to_use)>" name="<block_name>" template="<your_template_reference_path>"/>
        </referenceContainer>
    </body>
</page>

Sample code :

<?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">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="default_no_route" template="Magento_Cms::html/staticpage.phtml"/>
        </referenceContainer>
    </body>
</page>

If the controller does not exist, i.e. you want to show it on a new page. you need to create a controller and respective layout, router etc,. and use the above code in its layout. Just change referenceContainer tag to container.

  • The controller is exist in the path : /vendor/magento/module-cms/Controller/ But it is not working. – vinoth Apr 29 '17 at 11:38
  • So, do you want to display your block on home page. If yes, directly hit the url localhost/magento2/index.php (assuming your magento 2 setup is proper) – Pankaj Bhope Apr 29 '17 at 13:35

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.