如何在 symfony 1.4 中访问 actions.class.php 中的 POST 参数?

发布于 2024-09-10 20:50:28 字数 1590 浏览 3 评论 0原文

我对 symfony 还很陌生,所以如果这是一个愚蠢的问题,我很抱歉。我正在使用带有 Doctrine 的 symfony 1.4。我的同事编写了一个 JavaScript 来从客户端小部件向服务器发送报告:

$j.post(serverpath, {widget_id:widget_id, user_id:user_id, object_id:object_id, action_type:action_type, text_value:stuff_to_report });

我在routing.yml 中创建了一条路由来接收此请求:

widget_report:
  url: /widget/report/
  options: {model: ReportClass, type: object }
  param: {module: widget, action: reports}
  requirements:
    object_id: \d+
    user_id: \d+
    action_type: \d+
    sf_method: [post]

我在 actions.class.php 中创建了一个操作来处​​理该请求:

  public function executeReports(sfWebRequest $request) {
    foreach($request->getParameterHolder()->getAll() as $param => $val) {
        // $param is the query string name, $val is its value
        $this->logMessage("executeReports: $param is $val");  
    }
    try {
      [...]
     $actionHistory->setUserId($request->getParameter('user_id', 1));
     $this->logMessage("executeReports success: ");  
    } catch {
      [...]
    }
  }

我的日志文件报告:

Jul 20 18:51:35 symfony [info] {widgetActions} Call "widgetActions->executeReports()"
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports: module is widget
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports: action is reports
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports success: 

我一定是漏掉了一步。当在 URL 中传递变量(当然,并在路由中指定变量)时,我们可以使用此方法,但由于各种原因,我们希望改用 POST。

为什么我的 POST 参数在 actions.class.php 中无法访问?

I'm still fairly new to symfony, so my apologies if this is a stupid question. I'm using symfony 1.4 with Doctrine. My colleague wrote a JavaScript to make a report from our client side widget to our server:

$j.post(serverpath, {widget_id:widget_id, user_id:user_id, object_id:object_id, action_type:action_type, text_value:stuff_to_report });

I created a route in routing.yml to receive this request:

widget_report:
  url: /widget/report/
  options: {model: ReportClass, type: object }
  param: {module: widget, action: reports}
  requirements:
    object_id: \d+
    user_id: \d+
    action_type: \d+
    sf_method: [post]

I created an action in actions.class.php to handle the request:

  public function executeReports(sfWebRequest $request) {
    foreach($request->getParameterHolder()->getAll() as $param => $val) {
        // $param is the query string name, $val is its value
        $this->logMessage("executeReports: $param is $val");  
    }
    try {
      [...]
     $actionHistory->setUserId($request->getParameter('user_id', 1));
     $this->logMessage("executeReports success: ");  
    } catch {
      [...]
    }
  }

My log file reports:

Jul 20 18:51:35 symfony [info] {widgetActions} Call "widgetActions->executeReports()"
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports: module is widget
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports: action is reports
Jul 20 18:51:35 symfony [info] {widgetActions} executeReports success: 

I must be missing a step here. We had this working when passing the variables in the URL (and specifying the variables in the route, of course), but for various reasons we want to use POST instead.

Why are my POST parameters not accessible in actions.class.php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲性难收 2024-09-17 20:50:28

尝试此代码:

if ($request->isMethod('post')) {
    foreach($request->getPostParameters() as $param => $val) {
        $this->logMessage("executeReports: $param is $val");
    }
} else {
    $this->logMessage("executeReports: request method is not POST");
}

如果这没有帮助,请尝试:

$this->logMessage("executeReports: " . var_export($_POST, true));

或启用 symfony 调试工具栏并查看 POST 变量是否来自浏览器。

如果 $_POST 数组为空,则问题可能出在错误的请求标头中,要检查这一点,请尝试以下操作:

$fp = fopen('php://input','r');
$this->logMessage("executeReports: " . stream_get_contents($fp));

祝你好运!

编辑:

也许您可以在这里找到答案$.post 不发布任何内容。

例如,您应该检查所有 JS 变量是否不为空。

无论哪种情况,我都建议您使用 Firebug 控制台来查看发送到服务器的数据。

Try this code:

if ($request->isMethod('post')) {
    foreach($request->getPostParameters() as $param => $val) {
        $this->logMessage("executeReports: $param is $val");
    }
} else {
    $this->logMessage("executeReports: request method is not POST");
}

If this not helps, try:

$this->logMessage("executeReports: " . var_export($_POST, true));

Or enable symfony debug toolbar and see if POST vars are coming from browser.

If $_POST array is empty, then problem could be in wrong request headers, to check that, try this:

$fp = fopen('php://input','r');
$this->logMessage("executeReports: " . stream_get_contents($fp));

Good luck!

EDIT:

Maybe you can find your answer here $.post not POSTing anything.

E.g. you should check if all JS vars are not empty.

In either case, I'd recommend you to use Firebug console to see what data is sent to the server.

橘香 2024-09-17 20:50:28

为了回应 Sergiy,根据 jQuery 文档 (http://api.jquery.com/jQuery.ajax ),任何 jQuery ajax 请求的默认设置都是 application/x-www-form-urlencoded 和 UTF-8。 $.post 只是一个快捷方式,它将 ajax 'type' 设置为 'POST'。

可能是大小写不匹配,即 POST 与 post 吗?

In response to Sergiy, according to the jQuery documentation (http://api.jquery.com/jQuery.ajax), the default settings for any jQuery ajax request is application/x-www-form-urlencoded and UTF-8. $.post merely is a shortcut, which sets the ajax 'type' to 'POST'.

Could it possibly be a case mismatch, i.e. POST vs post?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文