dpl9717
2010-07-20 23:17 阅读 66

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

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dqkxo44488 dqkxo44488 2010-07-21 01:57

    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.

    点赞 评论 复制链接分享
  • dqb77047 dqb77047 2010-07-21 15:08

    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?

    点赞 评论 复制链接分享

相关推荐