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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)