Magento 2: How to get controller, module, action and router name?
Magento 2 – How to Get Controller, Module, Action, and Router Name
actioncontrollersmagento2modulerouter
Related Solutions
Again, what are you trying to do? Redirecting someone from sysconfig to your controller is odd UX at best, even if you redirect back to sysconfig after your code executes. If that's all you're doing, you should be consuming the POST and letting sysconfig behave as normal.
The reason your (first) redirect isn't working is that after this event fires, execution flows back to the controller action, which then sets its own redirect per usual:
class Mage_Adminhtml_System_ConfigController extends Mage_Adminhtml_Controller_Action
{
public function saveAction()
{
// snip ...
try {
// snip ...
Mage::dispatchEvent("admin_system_config_changed_section_{$section}",
array('website' => $website, 'store' => $store)
);
// snip ... }
catch (Mage_Core_Exception $e) {
// snip ...
}
catch (Exception $e) {
// snip ...
}
// snip ...
$this->_redirect('*/*/edit', array('_current' => array('section', 'website', 'store')));
}
You can get around this in a couple ways:
- (not ideal) Flush the response after setting the redirect in your browser; while this obeys your current intent, know that you are preventing subsequent native code from executing both in the action and in
postDispatch()
. - (better, but still pretty ugly) Instantiate your observer as a singleton, set a flag, and then set that observer class to also consume the
controller_action_postdispatch_adminhtml_system_config_save
event using a different method which will set your redirect.
E.g.
public function checkShouldRedirect()
{
$response = Mage::app()->getResponse();
//use 'mysection' to check we are in correct section
if (self::$flag==true && $this->getRequest()->getParam('section') == 'mysection') {
$response->setRedirect(
Mage::helper('adminhtml')->getUrl('myrouter/adminhtml_test/validate')
);
//below is only necessary for option 1
//$response->sendResponse();
}
}
If you are going to engage in the second option - and provided that you aren't opposed to the action saving the open panel state - you might as well just set your observer to consume only the postdispatch event, since you can test that you are saving your section by testing the section
param.
To answer your question, strictly from the coding point of view, you can do what Magento does:
// @see Mage_Core_Controller_Varien_Router_Standard::match
// instantiate controller class
$controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $front->getResponse());
so you could do something like:
// The action is overwritten in the action controller's constructor; to make sure
// there are no nasty surprises, we're going to undo this.
$originalAction = Mage::app()->getFrontController()->getAction();
// The action controller's constructor expects request and response objects;
$controllerInstance = Mage::getControllerInstance(
'TD_SMTPemail_Smtp_TestController',
new Mage_Core_Controller_Request_Http(), // you can replace this with the actual request
new Mage_Core_Controller_Response_Http()
);
$controllerInstance->testAction();
// and undo
Mage::app()->getFrontController()->setAction($originalAction);
Now, from the logical point of view, adding this kind of logic in the template is highly not recommended. The templates are only for output; they can use some logic, but the logic has to be contained in their block class, so in the template you can only call $this->???
methods; with the exception of some minor helpers maybe, something like currency formatting for instance.
If the controller action that sends the emails is yours, you could refactor it, move the email sending logic to a model, then you can use that model in both situations, the original controller and the current controller. If the controller is not yours, then you migth wanna take a look at what models it uses and re-use those.
Cheers
Best Answer
Use the below code in the controller class to get the controller, module, action and route name: