How to redirect to module/controller/action when I'm inside an observer method? I need to redirect to myrouter/adminhtml_test/validate.
Why I need this:
I try to fire my controller's action which has to perform some additional calculations after "Save Config" was clicked in System -> Configuration -> Mysection
. I registered observer for the event admin_system_config_changed_section_{$section}.
In my method handle_adminSystemConfigChangedSection()
in Model/Observer.php I try to fire my action but it doesn't redirect to that action:
class My_Module_Model_Observer
{
public function handle_adminSystemConfigChangedSection()
{
//I tried this code but it doesn't trigger the action:
$url = Mage::helper('adminhtml')->getUrl("myrouter/adminhtml_test/validate");
Mage::log($url); //To check if URL is correct (and it is correct)
Mage::app()->getResponse()->setRedirect($url);
}
}
Observer method and controller work fine (below are configuration details). I replaced the observer method with this code (from Magento forum) using header()
and it works:
public function handle_adminSystemConfigChangedSection()
{
$url = Mage::helper('adminhtml')->getUrl("myrouter/adminhtml_test/validate");
Mage::log($url);
header( 'Location: ' . $url );
exit();
}
But this doesn't seem to be a correct way of redirecting to an action. How it should be done?
Configuration details:
Observer and router configuration in config.xml:
<config>
...
<global>
...
<events>
<admin_system_config_changed_section_mysection>
<observers>
<mymodule>
<type>singleton</type>
<class>mymodule/observer</class>
<method>handle_adminSystemConfigChangedSection</method>
</mymodule>
</observers>
</admin_system_config_changed_section_mysection>
</events>
</global>
<admin>
<routers>
<mymodule>
<use>admin</use>
<args>
<module>My_Module</module>
<frontName>myrouter</frontName>
</args>
</mymodule>
</routers>
</admin>
...
</config>
Controller in controllers/Adminhtml/TestController.php:
class My_Module_Adminhtml_TestController extends Mage_Adminhtml_Controller_Action
{
public function validateAction()
{
Mage::log('Test: action is working!'); //To check if action is working
//do something here...
}
}
System > Configuration > Mysection
. In my methodhandle_adminSystemConfigChangedSection()
I try to fire my controller's action which has to perform some additional calculations.