Magento – how to call controller method from block file

controllerssystem-config

I am calling a method written in controller file from a block file as shown in below code–

BLOCK FILE

class Assel_Sms_Block_Adminhtml_Sendpushmsg extends Mage_Adminhtml_Block_System_Config_Form_Field
{


    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) 
    {
        $this->setElement($element);
        $buttonHtml = $this->_getAddRowButtonHtml($this->__('Send Push Message'));
        return $buttonHtml;
    }


  protected function _getAddRowButtonHtml($title)
  {

        $buttonBlock = $this->getElement()->getForm()->getParent()->getLayout()->createBlock('adminhtml/widget_button');

        $_websiteCode = $buttonBlock->getRequest()->getParam('website', null);

        $params = array();

        if(!empty($_websiteCode)) {
            $params['website'] = $_websiteCode;
        }

        // TODO: for real multi-store self-testing, the test button (and other configuration options) 
        // should probably be set to show in website. Currently they are not.
        $url = Mage::helper('adminhtml')->getUrl("*/pushmessage_sendpushmsg/sendpushnotification", $params);

        $buttonHtml = $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setType('button')
                    ->setLabel($this->__($title))
                    ->setOnClick("window.location.href='".$url."'")
                    ->toHtml();

        return $buttonHtml;    
    }    
}

In above file code $url = Mage::helper('adminhtml')->getUrl("*/pushmessage_sendpushmsg/sendpushnotification", $params); is calling my controller method.but it is showing 404 page.

What I gonna do is I want to call above controller on click a button on system configuration page. as shown in below image-

enter image description here

Here is controller method–

<?php

class Assel_Sms_Pushmessage_SendpushmsgController extends Mage_Adminhtml_Controller_Action {

    public function sendpushnotificationAction() {


         $_helper = Mage::helper('sms');
         $_helper->log($_helper->__("Could not find required database tables"));
         return;
    }   

} 

EDITED–

Config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
        <Assel_Sms>
            <version>1.0.2</version>
        </Assel_Sms>
   </modules>
    <admin>
        <routers>
            <sms>
                <use>admin</use>
                <args>
                    <module>Assel_Sms</module>
                    <frontName>sms</frontName>
                </args>
            </sms>
        </routers>
    </admin>
    <frontend>
        <routers>
            <sms>
                <use>standard</use>
                <args>
                    <module>Assel_Sms</module>
                    <frontName>sms</frontName>
                </args>
            </sms>
        </routers>
    </frontend>

    <global>
        <models>
            <sms>
                <class>Assel_Sms_Model</class>
            </sms>
        </models>
        <blocks>
            <sms>
                <class>Assel_Sms_Block</class>
            </sms> 
        </blocks>
        <helpers>
            <sms>
                <class>Assel_Sms_Helper</class>
            </sms>
        </helpers>
        <rewrite>        
            <sms_customer_accountcontroller>
                <from><![CDATA[#^/customer/account/#]]></from> 
                <to>/sms/customer_account/</to> 
            </sms_customer_accountcontroller>
        </rewrite>
        <events>
                <sales_order_place_after>
                    <observers>
                        <sms>
                            <type>singleton</type>
                            <class>sms/observer</class>
                            <method>sendSmsAfterOrder</method>
                        </sms>
                    </observers>
                </sales_order_place_after>
                <sales_order_invoice_save_after>
                    <observers>
                        <sms>
                            <type>singleton</type>
                            <class>sms/observer</class>
                            <method>sendSmsAfterInvoice</method>
                        </sms>
                    </observers>
                </sales_order_invoice_save_after>
                <sales_order_creditmemo_save_after>
                    <observers>
                        <sms>
                            <type>singleton</type>
                            <class>sms/observer</class>
                            <method>sendSmsAfterCreditmemo</method>
                        </sms>
                    </observers>
                </sales_order_creditmemo_save_after>
                <catalog_product_save_after>
                    <observers>
                        <sms>
                            <type>singleton</type>
                            <class>sms/observer</class>
                            <method>sendSmsNotifyMe</method>
                        </sms>
                    </observers>
                </catalog_product_save_after>
                <customer_register_success>
                    <observers>
                        <sms_customer_register_success>
                            <type>singleton</type>
                            <class>sms/observer</class>
                            <method>sendSmsOnCustomerCreate</method>
                        </sms_customer_register_success>
                    </observers>
                </customer_register_success>
                <sales_order_save_after>
                    <observers>
                        <sms>
                            <type>singleton</type>
                            <class>sms/observer</class>
                            <method>sendSmsOnOrderCancel</method>
                        </sms>
                    </observers>
                </sales_order_save_after>

                <controller_action_postdispatch_customer_account_forgotpasswordpost>
                    <observers>
                        <sms>
                            <class>sms/observer</class>
                            <method>sendSmsOnForgotPassword</method>
                        </sms>
                    </observers>
                </controller_action_postdispatch_customer_account_forgotpasswordpost>
        </events>
    </global>
</config>

Best Answer

Here the problem is you are defining a new admin route for your module and the frontname for that route is sms. Hence your controller will be listened only when a url : www.yourdomain.com/sms/pushmessage_sendpushmsg/sendpushnotification

Instead you need to rewrite admin route in your module. The code for that shown below.

<config>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <assel_sms before="Mage_Adminhtml">Assel_Sms</assel_sms>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Here what you are doing is rewriting admin router, so that you can use the default admin router frontendname admin. Hope that makes sense


Note 1 : if you use this line of code.

$url = Mage::helper('adminhtml')->getUrl("sms/pushmessage_sendpushmsg/sendpushnotification", $params);

then you will be able to use your current admin router.


Note 2 : Best practice in Magento says, you should put all admin controller in Adminhtml directory in order to distinguish them from frontend controllers. So in order to do that, you need to do two changes.

<config>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <assel_sms before="Mage_Adminhtml">Assel_Sms_Admintml</assel_sms>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

See the values inside assel_sms node. Now change your controller file accordingly.

File : app\code\local\Assel\Sms\controllers\Adminhtml\Pushmessage\SendpushmsgController.php

<?php

class Assel_Sms_Admihtml_Pushmessage_SendpushmsgController extends Mage_Adminhtml_Controller_Action {

    public function sendpushnotificationAction() {


         $_helper = Mage::helper('sms');
         $_helper->log($_helper->__("Could not find required database tables"));
         return;
    }   

} 
Related Topic