cancel
Showing results for 
Search instead for 
Did you mean: 

Controller can not find the template file

SOLVED

Controller can not find the template file

Hi I started larning Magento 2.3

I am developping an extension "Knc_Tips"

now menu is working, controller is fine maybe.

but, view file does not work. browser does not says any error but controller can not reach the view file I put.

 

etc/adminhtml/routes.xml

 

   <router id="admin">                                                                                                                                                                
        <route frontName="topic" id="topic">
            <module name="Knc_Tips"/>
        </route>
    </router>

 

 

Controller/Adminhtml/Posts/Index.php

 

public function execute()
{
    $resultPage = $this->pageFactory->create();
    $resultPage->getConfig()->getTitle()->prepend((__('Posts')));
    return $resultPage;
}

view/adminhtml/layout/topic_posts_index.xml

 

<?xml version="1.0"?>                                                                                                                                                                  
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head<
        <title>
            Posts
        </title>
    </head>
    <body>
        <div>test</div>
        <referenceContainer name="content">
            <block class="Magento\Backend\Block\Template" template="Knc_Tips::posts.phtml" />
        </referenceContainer>
    </body>
</page>

 

I put some html contents on topic_posts_index.xml but nothing come out on browser.

although there is resource container "posts.phtml", this also does not come up.

 

when I access this page, there is a page title "Posts" only. I think this is from Controller.

I thought Controller does not read the template file, so I changed the template file name to

view/adminhtml/layout/test.xml

i did cache clean, but still display does not change. not 404. just says page title "Posts".

I believe "Controller does not reach the layout file"

 

where do i have to check and pay attension to solve this problem?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Controller can not find the template file

Hi @reikokurose240 

 

So you have error in your layout (XML) file code, first is you have added <Head> tag wrong - check the syntax it is added like this - <head< 

 

So it will not work here, put the following content in your view/adminhtml/layout/topic_posts_index.xml file

 

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="page.title">
        <action method="setPageTitle">
            <argument name="title" xsi:type="string">Posts</argument>
        </action>
    </referenceBlock>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Backend\Block\Template" template="Knc_Tips::posts.phtml"/>
        </referenceContainer>
    </body>
</page>

Second, you need to create the template file as well, (PHTML File) wherein you can put your content which will load the template.

 

Create posts.phtml file in this location - view/adminhtml/templates/posts.html - Adding following content it it :

 

<p>My First Posts load from template</p>

Now run all the necessary commands again like setup:upgrade, compile and static content deploy then clear and flush cache and check!

 

It will work for you.

 

if issue solved,Click Kudos & Accept as Solution

View solution in original post

5 REPLIES 5

Re: Controller can not find the template file

Hi @reikokurose240 


Kindly refer below link:
https://devdocs.magento.com/guides/v2.4/ext-best-practices/extension-coding/example-module-adminpage...

In my case I have followed suggested steps and it works for me.

It may help you!
Problem Solved? Please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: Controller can not find the template file

Hi @reikokurose240 

 

So you have error in your layout (XML) file code, first is you have added <Head> tag wrong - check the syntax it is added like this - <head< 

 

So it will not work here, put the following content in your view/adminhtml/layout/topic_posts_index.xml file

 

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="page.title">
        <action method="setPageTitle">
            <argument name="title" xsi:type="string">Posts</argument>
        </action>
    </referenceBlock>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Backend\Block\Template" template="Knc_Tips::posts.phtml"/>
        </referenceContainer>
    </body>
</page>

Second, you need to create the template file as well, (PHTML File) wherein you can put your content which will load the template.

 

Create posts.phtml file in this location - view/adminhtml/templates/posts.html - Adding following content it it :

 

<p>My First Posts load from template</p>

Now run all the necessary commands again like setup:upgrade, compile and static content deploy then clear and flush cache and check!

 

It will work for you.

 

if issue solved,Click Kudos & Accept as Solution

Re: Controller can not find the template file

 
 
 

Re: Controller can not find the template file

 
I changed xml file along you showed me, then it works well.
I realized that the docs I saw was for old version.
 
 

Re: Controller can not find the template file

Hello @reikokurose240 

 

Glad to know that it works for you.

 

Happy to help and keep helping others Smiley Happy

if issue solved,Click Kudos & Accept as Solution