开发者

How do I access POST parameters in actions.class.php in symfony 1.4?

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 {
      [...]
    }
  }

M开发者_如何学Goy 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?


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜