简体   繁体   中英

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:

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

Try this:

use FOS\RestBundle\View\View; 

And then in the action:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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