简体   繁体   English

FOSRestBundle视图注释无效

[英]FOSRestBundle View annotation not working

config.yml:

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener: 
        default_priorities: [json, xml]
    view:
        view_response_listener: 'force'  

sensio_framework_extra:
    view:    { annotations: false }
    router:  { annotations: true }

annotated controller:

/**
 * @Rest\View(statusCode=204)
 * @param \Symfony\Component\HttpFoundation\Request $request
 */
public function signUpAction(Request $request)
{
    return $this->get('%%%.response_generator')->generateResponse(array('test'), true, 'test'); 
    // returns a simple Object with some public properties
}

If I access the route with .json format it returns: 如果我以.json格式访问该路由,则返回:

{"status":1,"message":"test","data":["test"]}

But it won't change the status code to the one from annotation. 但它不会将状态代码更改为注释中的状态代码。

Also without a specified format, it doesn't fallback to the default .json format, it still searches for a template. 同样没有指定的格式,它不会回.json默认的.json格式,它仍然会搜索模板。

Try this: 尝试这个:

use FOS\RestBundle\View\View; 

And then in the action: 然后在行动中:

$view = View::create($response, 204);
return $view;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM