Symfony FOSRestController空响应

时间:2015-11-19 12:20:19

标签: php symfony http-status-codes

我已经把方法和我想要在逻辑有一些响应“项目成功更新”后我使用statusCodes数字错误和描述但我有204无内容,当我的行动完成。但是什么时候

return "update old project"; 

但这是错的我想用状态代码和描述,为什么我没有回应不理解

Response Body [Raw]

"update old project"

我的行动

    /**
 * Update existing Projects from the submitted data or create a new Projects at a specific location.
 * @Put("/projects/{slug}", name="put", requirements={"id"="\d+"})
 * @ApiDoc(
 * resource = true,
 * description = "Create/Update single Projects",
 *  parameters={
 *      {"name"="level", "dataType"="string", "required"=true, "description"="project choice - Senior, Middle+, Middle, Middle/Junior, Junior"},
 *      {"name"="main_skill", "dataType"="string", "required"=true, "description"="mainskill project"},
 *      {"name"="name", "dataType"="string", "required"=true, "description"="additional information"},
 *      {"name"="duration", "dataType"="string", "required"=true, "description"="1 year+, 3 months+, 2 weeks"},
 *      {"name"="location", "dataType"="string", "required"=true, "description"="location project"},
 *      {"name"="expected_budget", "dataType"="string", "required"=true, "description"="expected_budget project"},
 *      {"name"="start_date", "dataType"="string", "required"=true, "description"="start date project"},
 *      {"name"="description", "dataType"="string", "required"=false, "description"="description"},
 *      {"name"="employment", "dataType"="string", "required"=true, "description"="employment"},
 *      {"name"="project_skills", "dataType"="array", "required"=true, "description"="project skills project"},
 *      {"name"="markets", "dataType"="array", "required"=true, "description"="project markets "},
 *      {"name"="platforms", "dataType"="array", "required"=true, "description"="project platforms "},
 *      {"name"="token", "dataType"="string", "required"=true, "description"="is equally md5('email'.secret_word)"},
 *      {"name"="speciality", "dataType"="array", "required"=true, "description"="project speciality"},
 *      {"name"="type", "dataType"="string", "required"=false, "description"="project type - fixed_cost or time_and_material"},
 *      {"name"="rate", "dataType"="integer", "required"=true, "description"="project rate"}
 *
 *  },
 * statusCodes = {
 * 200 = "Projects successful update",
 * 404 = "Return when project with current slug not isset",
 * 101 = "Secret token is not valid"
 * },
 *  section="Project"
 * )
 * @RestView()
 *
 * @param  Request $request
 * @param  string $slug
 *
 * @return View
 */
public function putProjectAction(Request $request, $slug)
{
    $manager = $this->getDoctrine()->getManager();
    $token = $this->get('request')->request->get('token');
    $user = $this->getDoctrine()->getRepository('ArtelProfileBundle:Users')->findOneBySecuritytoken($token);
    $projectOld = $manager->getRepository('ArtelProfileBundle:Project')
        ->putProjectClient($slug, $user);
    $data = $request->request->all();
    $view = View::create();
    if(empty($projectOld)){
        $view->setData('not find user project');
    }
    if(empty($user)){
        $view->setData('not find user');
    }
    if (!empty($user)) {
        $data = $this->get('serializer')->serialize($data, 'json');

        $projectOld = $manager->getRepository('ArtelProfileBundle:Project')
            ->findOneBySlug($slug);
        $projectNew = $this->get('serializer')
            ->deserialize($data, 'Artel\ProfileBundle\Entity\Project', 'json');
        if (!$projectOld) {
            $projectNew->setUserId($user);
            $projectNew->setCurrentStatus('not_approved');
            $projectNew->setStrippedName();
            $manager->persist($projectNew);
            $manager->flush();
            $view->setStatusCode(404);
        } else {
            $this->get('artel.project.update')->updateObject($projectOld, $projectNew);
            $manager->flush();
            $view->setStatusCode(200);
        }
    }else{
        $view->setStatusCode(101);
    }
    return $view;
}

我用sanbox测试它,因为如果我测试PUT方法,postman没有在请求中发送参数,POST发送参数就行动了。

1 个答案:

答案 0 :(得分:0)

这是一个API吗? 如果是这样的话,¿为什么不使用Response对象? 您可以设置响应代码,如果您愿意使用Serializer来提供xml或json等格式。

fundamentals中,您可以找到我的意思

例如:

  

返回新的响应($ this-> container-> get('serializer') - > serialize($ access,'json'),200,array(“Content-Type”=>“application / JSON;字符集= UTF-8" ));

这将返回json格式的响应,代码200和内容类型“application / json”

希望有所帮助