简体   繁体   中英

How to call controller from custom menu created by extension in Magento?

I am new to Magento and at first up i got the task to create Magento Extension. So i am reading from the internet and creating extension. My Magento version is 1.7.0.2.

I am able to create basic structure of extension and kept config.xml and other files at proper place which made my extension to install properly in Magento but now i want to call my controller from my menu which i added in main navigation in admin section. This is my config.xml file, please tell me what i am doing wrong here which is not letting me call my controller from my menu.

<?xml version="1.0"?>
<config>
<!-- turn on our module, required for install support -->
<modules>
<Gwb_Magecrmsync>
    <version>0.1.0</version>
</Gwb_Magecrmsync>
</modules>
<global>
<helpers>
    <magecrmsync>
        <class>Gwb_Magecrmsync_Helper</class>
    </magecrmsync>
</helpers>
<!-- turn on models -->
<models>
    <magecrmsync>
        <class>Gwb_Magecrmsync_Model</class>
        <resourceModel>Magecrmsync_mysql4</resourceModel>
    </magecrmsync>
</models>
<!-- turn on models -->

<!-- turn on database connections -->
<resources>
<!-- setup is needed for automatic installation -->
    <magecrmsync_setup>
        <use>default_setup</use>
    </magecrmsync_setup>
    <magecrmsync_write>
        <use>default_write</use>
    </magecrmsync_write>
    <magecrmsync_read>
        <use>default_read</use>
    </magecrmsync_read>
</resources>
<blocks>
    <magecrmsync>
        <class>Gwb_Magecrmsync_Block</class>
    </magecrmsync>
</blocks>
<layout>
    <magecrmsync>
        <file>Magecrmsync.xml</file>
    </magecrmsync>
</layout>
</global>
<admin>
<routers>
    <magecrmsync>
        <use>admin</use>
        <args>
            <module>Gwb_Magecrmsync</module>
            <frontName>magecrmsync</frontName>
        </args>
    </magecrmsync>
</routers>
</admin>
<adminhtml>
<menu>
    <menu1 translate="title" module="magecrmsync">
        <title>Synchronize</title>
        <sort_order>999</sort_order>
        <children>
            <menuitem1 module="magecrmsync">
                <title>Synchronize</title>
                <action>magecrmsync/adminhtml_magecrmsync</action>
            </menuitem1>
        </children>
    </menu1>
</menu>
<acl>
    <resources>
        <admin>
            <children>
                <menu1 translate="title" module="magecrmsync">
                    <title>Synchronize</title>
                    <sort_order>999</sort_order>
                    <children>
                        <menuitem1>
                            <title>Synchronize</title>
                        </menuitem1>
                    </children>
                </menu1>
            </children>
        </admin>
    </resources>
</acl>
</adminhtml>
</config>

Please check my updated code here :

How to add custom tab in left sidebar to my custom page in admin section in magento module?

Any help would be appreciated.

Thanks

You should try not to use all capitals for a namin convension, in some cases Captial letters are used to distinguish different classes etc, rename to:

Gwb_Magecrmsync

Your action is incorrect, try this:

<action>magecrmsync/adminhtml_synchronize</action>

You also need a Router definition for the admin

<admin>
    <routers>
        <magecrmsync>
            <use>admin</use>
            <args>
                <module>GWB_MAGECRMSYNC</module>
                <frontName>magecrmsync</frontName>
            </args>
        </magecrmsync>
    </routers>
</admin>

I would recommend that you change your module name from MAGECRMSYNC to MageCrmSync

Try

.....
<admin>
    <routers>
        <magecrmsync>
            <use>admin</use>
            <args>
                <module>GWB_MAGECRMSYNC</module>
                <frontName>magecrmsync</frontName>
            </args>
        </magecrmsync>
    </routers>
</admin>
<adminhtml>
<menu>
    <magecrmsync translate="title" module="magecrmsync">
        <title>Synchronize</title>
        <sort_order>60</sort_order>
        <children>
            <menuitem module="magecrmsync">
                <title>Menu item 1</title>
                <action>magecrmsync/adminhtml_synchronize</action>
            </menuitem>
        </children>
    </magecrmsync>
</menu>
<acl>
    <resources>
       <all>
            <title>Allow Everything</title>
        </all>
        <admin>
            <children>
                <magecrmsync translate="title" module="magecrmsync">
                    <title>Sychronize</title>
                    <sort_order>60</sort_order>
                    <children>
                        <menuitem>
                            <title>Menu item 1</title>
                        </menuitem>
                    </children>
               </magecrmsync>
           </children>
       </admin>
   </resources>
</acl>
<adminhtml>
</config>

Have a look at my config maybe it will help you.

<?xml version="1.0"?>
<config>
    <modules>
        <Tibdev_Paypal>
            <version>1.1.0</version>
        </Tibdev_Paypal>
    </modules>

    <global>
        <models>
           ...
        </models>

        <helpers>
            ...
        </helpers>

        <resources>
            ...
        </resources>

    </global>

    <admin>
        <routers>
            ...
        </routers>
    </admin>


    <adminhtml>
        <menu>
            <menu1 translate="title" module="Paypal">
                <title>Tigerbytes GmbH</title>
                <sort_order>999</sort_order>
                <children>
                    <menuitem1 module="Paypal">
                        <title>Paypal 10413 Fix</title>
                        <action>tibdevpaypal/adminhtml_form</action>
                    </menuitem1>
                </children>
            </menu1>
        </menu>
        <acl>
            <resources>
                <admin>
                    <children>
                        <menu1 translate="title" module="Paypal">
                            <title>Tigerbytes GmbH</title>
                            <sort_order>999</sort_order>
                            <children>
                                <menuitem1>
                                    <title>Paypal 10413 Fix</title>
                                </menuitem1>
                            </children>
                        </menu1>
                    </children>
                </admin>
            </resources>
        </acl>


        <layout>
            <updates>
                ...
            </updates>
        </layout>

        <translate>
            <modules>
                ...
            </modules>
        </translate>

    </adminhtml>

</config>

并将您的模块重命名为

<Gwb_Magecrmsync>

Check these SO questions that Alan Storm answered for some guidance, too:
Magento Custom Module How to store variable in config.xml
and Setting a global variable in Magento, the GUI way?

The second question has more thorough answers, one of which helped me with the first module I have with Admin menus. I added an answer to the 2nd question, attempting to further explain what I was doing, and included the package I created related to my answer.

This is how the config.xml should be :

<?xml version="1.0"?>
<config>
<!-- turn on our module, required for install support -->
<modules>
<Gwb_Magecrmsync>
    <version>0.1.0</version>
</Gwb_Magecrmsync>
</modules>
<global>
<helpers>
<magecrmsync>
    <class>Gwb_Magecrmsync_Helper</class>
</magecrmsync>
</helpers>
<!-- turn on models -->
<models>
<magecrmsync>
    <class>Gwb_Magecrmsync_Model</class>
    <resourceModel>Magecrmsync_mysql4</resourceModel>
</magecrmsync>
</models>
<!-- turn on models -->

<!-- turn on database connections -->
<resources>
<!-- setup is needed for automatic installation -->
<magecrmsync_setup>
    <use>default_setup</use>
</magecrmsync_setup>
<magecrmsync_write>
    <use>default_write</use>
</magecrmsync_write>
<magecrmsync_read>
    <use>default_read</use>
</magecrmsync_read>
</resources>
<blocks>
<magecrmsync>
    <class>Gwb_Magecrmsync_Block</class>
</magecrmsync>
</blocks>
<layout>
<magecrmsync>
    <file>Magecrmsync.xml</file>
</magecrmsync>
</layout>
</global>
<admin>
<routers>
<magecrmsync>
    <use>admin</use>
    <args>
        <module>Gwb_Magecrmsync</module>
        <frontName>magecrmsync</frontName>
    </args>
</magecrmsync>
</routers>
</admin>
<adminhtml>
<menu>
<menu1 translate="title" module="magecrmsync">
    <title>Synchronize</title>
    <sort_order>999</sort_order>
    <children>
        <menuitem1 module="magecrmsync">
            <title>Synchronize</title>
            <action>magecrmsync/adminhtml_synchronize</action>
        </menuitem1>
    </children>
</menu1>
</menu>
<acl>
<resources>
    <admin>
        <children>
            <menu1 translate="title" module="magecrmsync">
                <title>Synchronize</title>
                <sort_order>999</sort_order>
                <children>
                    <menuitem1>
                        <title>Synchronize</title>
                    </menuitem1>
                </children>
            </menu1>
        </children>
    </admin>
</resources>
</acl>
</adminhtml>
</config>

And after that create a SynchronizeController.php in Gwb/Magecrmcync/controllers/Adminhtml :

class Gwb_Magecrmsync_Adminhtml_SynchronizeController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
}

And it will work the way you want it.

Dude let me show you the way I am the future Magento expert. So the expert says:

In your config

<menu1 translate="title" module="magecrmsync">
        <title>Synchronize</title>
        <sort_order>999</sort_order>
        <children>
            <menuitem1 module="magecrmsync">
                <title>Synchronize</title>
                <action>magecrmsync/adminhtml_magecrmsync</action>
            </menuitem1>
        </children>
    </menu1>
  1. action node represent your controller So here your directory has to be in Magecrmsync/controllers/Adminhtml/MagecrmsyncController *make sure it's extending the right class

now in this controller you will call the loadlayout() -> this guy just load all the layout.xml files and keep it in memory and renderlayout() -> this guy do the actual printing of the stuff.

  1. But wait a minute. loadlayout() above does not know about our layout.xml of this our custom extension. No problem lets create one. So simply go to design->adminhtml->package->theme->layout and place your layout.xml which in your case is .... is..... oh you don't have that in your config.xml (because layout.xml for admin has to be in adminhtml node, you have one but that seems to be for frontend but still that not right either because you don't have front end node.) Anyways lets assume you you might have got this by now and got a new layout xml file for you admin and placed it in adminhtml node. for example

     <adminhtml> <layout> <updates> <services> <file>layitoutdarling.xml</file> </services> </updates> </layout> </adminhtml> 

Now this our new layoutitoutdarling file will reference or we can say points to classes which we need for our journey. This layout file will hold the secret to what you need. yes what it gonna have ????? it wil have the ... the .. Handles :-S Handles match you URL (or path to your mod/controller/method) and call the stuff enclosed in between them. eg in this random example is a handle, when you go to that url block class will be called whose path is Employee/Block/Adminhtml/Employee.php

<?xml version="1.0"?>
<layout version="0.1.0">
    <employee_adminhtml_employee_index>
        <reference name="content">
            <block type="employee/adminhtml_employee" name="employee" />
        </reference>
    </employee_adminhtml_employee_index>
</layout>

Now actually this file(Employee/Block/Adminhtml/Employee.php) is your Grid container. Then Grid container will points you to Grid file(Employee/Block/Adminhtml/Employee/Grid.php). There is a difference between a Grid Container and Grid.

Forms in magento are divided in two 4 basic parts 1. FORM Container 2. FORM tag 3. FORM Tabs 4. Actual Form Fields

More info on whatever I have said is here

Then for Tabs Suppose you have placed Add New Sexy Girl button in the Grid Container section. Of Course it will point to a (controller) /sexyController/newAction

So now your newAction will points you to add the tabs on the left handside.

public function newAction(){
$this->loadLayout();
$this->_addContent($this->getLayout()->createBlock('form/adminhtml_form_edit'))
(_addLeft() here we are adding tabs)
                ->_addLeft($this->getLayout()->createBlock('form/adminhtml_form_edit_tabs'));
$this->renderLayout();
}

Detailed information on how to add tabs and more click here

Yea I just had 4 beers and still have 2 left.

Rest my dear friend check that website and you will also become like me .. a future magento expert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM