I need controller method in Observer
My observer :
public function observer()
{
//need controller action here
}
My Controller:
public function adminAction()
{
//my action
}
My questions
You can instantiate a controller anywhere with
$controller = Mage::getControllerInstance(
'The_Controller_Class',
Mage::app()->getRequest(),
Mage::app()->getResponse());
To tell if this is the best way, I would need to know more about your use case, but it probably isn't. If you want to show a different page without redirecting, have a look at Mage_Core_Controller_Varien_Action::_forward()
:
/**
* Throw control to different action (control and module if was specified).
*
* @param string $action
* @param string|null $controller
* @param string|null $module
* @param array|null $params
*/
protected function _forward($action, $controller = null, $module = null, array $params = null)
{
$request = $this->getRequest();
$request->initForward();
if (isset($params)) {
$request->setParams($params);
}
if (isset($controller)) {
$request->setControllerName($controller);
// Module should only be reset if controller has been specified
if (isset($module)) {
$request->setModuleName($module);
}
}
$request->setActionName($action)
->setDispatched(false);
}
Unfortunately this is protected so it can only be used from within a controller. But you can do the same from anywhere else. What is happening here:
Mage::app()->getRequest()
) is backed up: $request->initForward()
If your observer is attached to an event that takes place before dispatching, it is sufficient to change the request parameters.
Yes, you can listen on dynamic events:
controller_action_predispatch_' . $this->getFullActionName()
orcontroller_action_postdispatch_' . $this->getFullActionName()
You can find the dispatch of these events in app/code/core/Mage/Core/Controller/Varien/Action.php
The method $this->getFullActionName()
returns a string formed by routeName_controllerName_actionName
so, assuming that your routeName is myadminroute
, your controllerName is mycontroller
and your action is admin
(like your code) you can listen on the events:
controller_action_predispatch_myadminroute_mycontroller_admin
andcontroller_action_postdispatch_myadminroute_mycontroller_admin
If you are in the admin area you can listen on:
controller_action_predispatch_adminhtml_myadminroute_mycontroller_admin
andcontroller_action_postdispatch_adminhtml_myadminroute_mycontroller_admin
in your observer class you can retrieve the action controller by doing:
$observer->getControllerAction()
Example:
Supposing you are in admin area:
<config>
...
<models>
<my_model_prefix>
<class>MyNamespace_MyModule_Model</class>
</my_model_prefix>
</models>
...
<events>
<controller_action_predispatch_adminhtml_myadminroute_mycontroller_admin>
<observers>
<my_event_unique_tag>
<type>singleton</type>
<class>my_model_prefix/observer</class>
<method>myObserverMethod</method>
</my_event_unique_tag>
</observers>
</controller_action_predispatch_adminhtml_myadminroute_mycontroller_admin>
</events>
...
</config>
in Model/Observer.php
:
class MyNamespace_MyModule_Model_Observer
{
public function myObserverMethod($observer)
{
$controller = $observer->getControllerAction();
// do some stuff
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With