【发布时间】:2012-02-16 01:24:00
【问题描述】:
背景:
我正在 symfony 上编写一个 RESTful API。我希望客户端能够使用内容类型 application/json 发布到 url,并发布控制器操作正在寻找的 json 表单对象。
我为此使用了一个非常基本的控制器设置。出于演示目的,我们假设我尝试验证一个简单的用户名密码组合。
public function loginAction( Request $request )
{
$user = new ApiUser();
$form = $this->createForm(new ApiUserType(), $user);
if ( "POST" == $request->getMethod() ) {
$form->bindRequest($request);
if ( $form->isValid() ) {
$em = $this->getDoctrine()->getEntityManager();
$repo = $this->getDoctrine()->getRepository('ApiBundle:ApiUser');
$userData = $repo->findOneByUsername($user->getUsername());
if ( is_object($userData) ) {
/** do stuff for authenticating **/
}
else{
return new Response(json_encode(array('error'=>'no user by that username found')));
}
else{
return new Response(json_encode(array('error'=>'invalid form')));
}
}
}
现在我遇到的问题是,我是否尝试过 var_dumping 这个直到奶牛回家,无论出于何种原因,symfony 都不想获取应用程序/json 内容体并使用该数据来填充表单数据。
表单名称:api_apiuser
字段:用户名、密码
处理此类任务的最佳方式是什么。只要我能得到这个工作,我愿意接受建议。感谢您抽出宝贵时间。
【问题讨论】: