php 在 Magento 观察者中获取 POST 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22324889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:50:17  来源:igfitidea点击:

Getting POST data in Magento observer

phpmagento

提问by mludd

So, I'm struggling with this, I've got an observer set up to trigger whenever customer/account/login is hit. Firebug clearly shows that I'm POSTing data to this URL and I'm not able to read said POST data in my observer method.

所以,我正在为此苦苦挣扎,我已经设置了一个观察者,以便在客户/帐户/登录被点击时触发。Firebug 清楚地表明我正在将数据发布到此 URL,但我无法在我的观察者方法中读取所述 POST 数据。

Observer method:

观察者方法:

public function checkCustomerLogin($observer) {
    Mage::log("event observed");
    $controller = $observer->getControllerAction();
    Mage::log(print_r($controller->getRequest()->getPost(), true));
    return $this;
}

Example log result:

示例日志结果:

2014-03-11T11:46:38+00:00 DEBUG (7): event observed
2014-03-11T11:46:38+00:00 DEBUG (7): Array
(
)

My observer is configured to trigger on controller_action_predispatch_customer_account_login. Clearly I'm doing something wrong here seeing as how I just can't get my POST data (I've tried a few other desperate approaches but from what I can tell this is how you're "supposed" to get a controller and the POST data in an observer method).

我的观察者配置为在 上触发controller_action_predispatch_customer_account_login。显然,我在这里做错了什么,因为我无法获得我的 POST 数据(我尝试了其他一些绝望的方法,但据我所知,这是您“应该”获得控制器的方式和观察者方法中的 POST 数据)。

回答by Mohit Kumar Arora

Use Mage::app()->getRequest()->getParams()

Mage::app()->getRequest()->getParams()

It will return array of all parameters sent to called controller's action

它将返回发送到被调用控制器动作的所有参数的数组

Hope this helps you

希望这对你有帮助

回答by Oscprofessionals

instead of controller use app so

而不是控制器使用应用程序所以

instead of

代替

Mage::log(print_r($controller->getRequest()->getPost(), true));

change to

改成

Mage::log(print_r(Mage::app()->getRequest()->getPost(), true));

So that instead of controller you are using $app to get post details.

这样您就可以使用 $app 来获取帖子详细信息而不是控制器。