Symfony2 RestBundle无法获得POST参数

时间:2014-04-03 03:27:28

标签: php symfony fosrestbundle

我扩展了一个FOSRestController并尝试接收POST请求,但我无法获取该参数,并且对该API端点的所有请求都会导致错误:

Controller MapmsisdnController::postMapmsisdnAction() requires that you provide a value for the $deviceId argument (because there is no default value or because there is a non optional argument after this one). (500 Internal Server Error)

这是我的班级和注释

class MapmsisdnController extends FOSRestController
{
    /**
     * Map a deviceid to msisdn
     * @ApiDoc(
     *  resource = true,
     *  description = "Map an msisdn for a single deviceid",
     *  statusCodes = {
     *      200 = "Returned when successful",
     *      500 = "Internal Server Error"
     *  }
     * )
     * @Post("/mapmsisdn")
     * @param type $deviceId
     * @param type $msisdn
     */
    public function postMapmsisdnAction($deviceId, $msisdn)
    {
        $test = true;
        if ( $test ) {
            $result = array(
                "code" => 200,
                "message" => "ok",
                "deviceId" => $deviceId,
                "msisdn" => $msisdn
            );
            $serializer = $this->container->get('jms_serializer');
            $ret = $serializer->serialize($result, 'json');
            return new Response($ret);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在注释中,你应该指定@Post,并使用@RequestParam逐个指定你的参数:

@Post("/mapmsisdn")
@RequestParam(name="deviceId", requirements="\d+", default="", description="Device ID.")
@RequestParam(name="msisdn", requirements="\d+", default="", description="MSISDN.")

然后您可以检索如下参数:

$request = $this->getRequest();
$request->get('deviceId');
$request->get('msisdn');

@RequestParam是这样的:

use FOS\RestBundle\Controller\Annotations\RequestParam;