Magento 1 – Configure Custom Admin Module to Show .phtml File

adminadminhtmlconfigurationcontrollersmodule

I try to set up my admin module. I was using this this tutorial. Now when I click my new tabs in admin area the URL looks like this http://.../index.php/admin/custom/index/key/2157a41080bc0ffea9f9970b137e2aa8/ and I see a white screen. I want to load the content of a phtml file there.

My attempt so far includes following files:

  • /app/code/community/Petra/DailyOrders/controllers/Adminhtml/CustomController.php
  • /app/code/community/Petra/DailyOrders/etc/adminhtml.xml
  • /app/code/community/Petra/DailyOrders/etc/config.xml
  • /app/code/community/Petra/DailyOrders/Helper/Data.php (is empty)
  • /app/etc/modules/Petra_DailyOrders.xml

with the following contents:


CustomController.php

<?php class Petra_DailyOrders_Adminhtml_CustomController extends Mage_Adminhtml_Controller_Action {
public function indexAction()
{
    $this->loadLayout()
        ->_setActiveMenu('mycustomtab')
        ->_title($this->__('Bestellungen ansehen'));

    // my stuff

    $this->renderLayout();
}
}

adminhtml.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<menu>
    <mycustomtab>
        <title>Bestellungen/Tag</title>
        <sort_order>23</sort_order>
        <children>
            <index>
                <title>Anschauen</title>
                <sort_order>1</sort_order>
                <action>adminhtml/custom/</action>
            </index>               
        </children>
    </mycustomtab>
</menu>
<acl>
    <resources>
        <admin>
            <children>
                <custom>
                    <title>My Controller</title>
                    <sort_order>-100</sort_order>
                    <children>
                        <index>
                            <title>Index Action</title>
                            <sort_order>1</sort_order>
                        </index>                           
                    </children>
                </custom>
            </children>
        </admin>
    </resources>
</acl>
</config>

config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Petra_DailyOrders>
        <version>1.0.0</version>
    </Petra_DailyOrders>
</modules>
<global>
    <helpers>
        <petra_dailyorders>
            <!-- Helper definition needed by Magento -->
            <class>Mage_Core_Helper</class>
        </petra_dailyorders>
    </helpers>
</global>
<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <petra_dailyorders before="Mage_Adminhtml">Petra_DailyOrders_Adminhtml</petra_dailyorders>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
</config>

Petra_DailyOrders.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Petra_DailyOrders>
            <active>true</active>
            <codePool>community</codePool>
        </Petra_DailyOrders>
    </modules>
</config>

I would now add the phtml file to:
app/design/adminhtml/default/default/template/dailyorders/index.phtml

and an xml file to
app/design/adminhtml/default/default/layout/dailyorders.xml

and reference the dailyorders.xml within config.xml inside <layout></layout> tags.
Is there more to do than this, cause I already tried those steps many times in different ways, always resulting in an error "page not found".

Update

I added this piece of code to my config.xml

<adminhtml>
 <layout>
        <updates>
      <petra>
          <file>dailyorders.xml</file>
         </petra>
     </updates>   
    </layout>
</adminhtml>

and created the dailyorders.xml with the following code

<?xml version="1.0"?>
<layout version="0.1.0">
  <petra_adminhtml_index_index>    
    <reference name="content">
        <block type="adminhtml/template" name="dailyorders" template="dailyorders/index.phtml" />
    </reference>
  </petra_adminhtml_index_index>
</layout>

as well as an index.phtml with a simple <p> TEST </p> in it.

But nothing shows up. At least I don't have a file not found error this time. What do I have to change, to make this work, and show me the TEST paragraph?

Best Answer

This should be the content of your dailyorders.xml:

<?xml version="1.0"?>
<layout version="0.1.0">
    <adminhtml_custom_index>
        <reference name="content">
            <block type="adminhtml/template" name="dailyorders" template="dailyorders/index.phtml" />
        </reference>
    </adminhtml_custom_index>
</layout>

That's because the layout handle is <route>_<controller>_<action>.

You used the adminhtml router and created the CustomController.