4

I am creating module and I have added button to my module configuration section which have to call one of my methods.

So when I click button, it should call controller for example: localhost/index.php/admin/framexport

in config.xml, after i added:

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <namespace_mymodule before="Mage_Adminhtml">Namespace_Mymodule</namespace_mymodule>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin> 

I created at Namespace/Mymodule/controllers/FramexportController.php with code:

class Namespace_Mymodule_FramexportController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        Mage::log("TEST", null, 'test.log');
    }
}

when I try to go "localhost/index.php/admin/framexport/" or "localhost/index.php/admin/framexport/index/" it redirects me to dashboard and indexAction() is not called - no logfile created.

What am I missing or maybe I misunderstanded some part of code?

4 Answers 4

8

Magento has a security measure against CSRF attaks.
Each admin url must have a key parameter which is built from the url of the page and a random string generated each time you log in.
To get the url for your page (including the key) you need to call this Mage::helper('adminhtml')->getUrl('adminhtml/framexport/index').

But you can disable this security measure from System->Configuration->Admin->Security->Add Secret Key to URLs. But I don't recommend you to do this.

1
  • Thank you very much. That was thing I was missing. After turning this feature off controller was working so I enabled it again and added "Mage::helper('adminhtml')->getUrl('adminhtml/framexport/index')" to my button URL redirect. @musicliftsme rather I need to implement this as admin controller because it is used at backend configuration section.
    – Furoris
    Commented Mar 23, 2015 at 8:06
1

As Marius said, you're missing the key. You know your controller action is at least configured properly if you're not hitting a 404 response. So, that's a good start.

FYI, if you don't need the admin front name, you can make a regular frontend controller, which does not require a key in the URL, also.

<frontend>
    <routers>
        <my_controller>
            <use>standard</use>
            <args>
                <module>Namespace_Mymodule</module>
                <frontName>some_front_name</frontName>
            </args>
        </my_controller>
    </routers>
</frontend>

Then, you can access your action via localhost/index.php/some_front_name/framexport/index.

1

If you are using simple post action using the form action then you have to create the below hidden tag inside the form tag like below that is formKey which identify your request in custom controller.

<form action="url of controller fuction" name="" method="post">
// here will go othere fields of formms ...,  you can put this hidden fields anywhere between the form tag
<input name="form_key" id='form_key' type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" >
</form>

If you are using ajax to post your data and get response using ajax then you should use this like below , you can put the above hidden fields anywhere in page in case of ajax

Put this any where inside a page 
  <input name="form_key" id='form_key' type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" >

<scripts>

function updateMessages(msg, product_id){
    var form_key = jQuery('#form_key').val();   
    if(product_id != '' ){
        jQuery('.loading-overlay').show(); // this is most important to get values from hidden field
        jQuery.ajax({
            type:'POST',
            data:'product_id='+product_id+'&msg_val'+msg+'&form_key='+form_key,         
            url:'<?php echo $this->getUrl('verification/adminhtml_messages/update/'); ?>',
            success:function(data){
                var added_section = 'Message updated successfuly';
                jQuery('#messages').html(added_section);
            }
        });
    }
}
</scripts>
1

We have use custom form action in magento shop back. Example:

<form action='http://magento.com/admin/mymodule/test' method='POST' id='form' name='form'>

Use this above coding redirect to admin dashboard page. So now we have use below coding are:

<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />

So Now redirect to correct URL path......

test1111

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.