Magento – Magento 1.9 add layout and template to custom module in admin

adminhtmllayoutmagento-1.9moduletemplate

When I try to create a custom module that shows two tabs on the main page with the option to change titles, backgrounds, and colors, I was successful in adding the module's tab in the admin area but wasn't able to add the layout and template.

any one can explain please , how i can do this?

here the xml files:

app/etc/modules/Lern_Sample.xml/

 <?xml version="1.0"?>
<config>
<modules>
    <Lern_Sample>
        <active>true</active>
        <codePool>local</codePool>
        <depends>
            <Mage_Adminhtml />
        </depends>
    </Lern_Sample>
</modules>

app/code/local/Lern/Sample/Block/Adminhtml/Sample/

grid.php

 `class Lern_Sample_Block_Adminhtml_Sample_Grid extends 
 Mage_Adminhtml_Block_Widget_Grid
  {
    public function __construct()
  {
       parent::__construct();

  }
 }`

Sample.php

   class Lern_Sample_Block_Sample extends Mage_Payment_Block_Form
   {
      public function __construct() {}
   }

app/code/local/Lern/Sample/controllers/Adminhtml/IndexController.php

   class Lern_Sample_Adminhtml_IndexController extends 
 Mage_Adminhtml_Controller_Action {

   public function indexAction() {

      $this->loadLayout();
      $this->renderLayout();

   }
}

design/adminhtml/default/default/layout/sample.xml

        <?xml version="1.0"?>
         <layout version="0.1.0">
          <lern_sample_index_index>
           <reference name="root">
             <action method="setTemplate"><template>page/1column.phtml</template></action>
            </reference>
           <reference name="content">
            <block type="Lern_Sample/index" name="Lern_Sample_index" template="sample/sample.phtml"/>
           </reference>
          </lern_sample_index_index>
         </layout>

design/adminhtml/default/default/template/sample.phtml

Hello World

config.xml

       <?xml version="1.0"?>
    <config>
     <modules>
    <Lern_Sample>
        <version>0.1.0</version>
    </Lern_Sample>
</modules>
<frontend>
    <routers>
        <sample>
            <use>standard</use>
            <args>
                <module>Lern_Sample</module>
                <frontName>sample</frontName>
            </args>
        </sample>
    </routers>
</frontend>
<admin>
    <routers>
        <sample>
            <use>admin</use>
            <args>
                <module>Lern_Sample</module>
                <frontName>admin_sample</frontName>
            </args>

        </sample>
    </routers>
</admin>
<global>
    <helpers>
        <sample>
            <class>Lern_Sample_Helper</class>
        </sample>
    </helpers>
</global>
<adminhtml>
    <menu>
        <sample module="sample">
            <title>Sample Module</title>
            <sort_order>100</sort_order>
            <children>
                <sample module="sample">
                    <title>Sample Module</title>
                    <sort_order>0</sort_order>
                    <action>admin_sample/adminhtml_index</action>

                    <layout>
                        <lern_sample>
                            <file>lern_sample.xml</file>
                        </lern_sample>
                    </layout>
                </sample>
            </children>
        </sample>
    </menu>
</adminhtml>

Best Answer

Below code is to add xml in your admin module:

<adminhtml>
    <layout>
        <updates>
            <lern_sample>
                <file>lern_sample.xml</file>
            </lern_sample>
        </updates>
    </layout>
</adminhtml>

You need to add it under adminhtml not menu. Below is the code for your layout xml (lern_sample.xml):

<?xml version="1.0"?>   
<layout version="0.1.0">   
    <module_index_index>   
        <reference name="root">   
            <action method="setTemplate"><template>page/1column.phtml</template></action>   
        </reference>   
        <reference name="content">   
            <block type="module/index" name="module_index" template="module/index.phtml"/>   
        </reference>   
    </module_index_index>   
</layout>