Search code examples
symfonyfosrestbundle

How to set http status code explicitly with FOSRestBundle


I'm using FOSTRestBundle and I'd like set the status code not statically like show in the documentation by using annotation:

/**
 * @View(statusCode=204)
 */
public function deleteUserAction()
{
    // ...
}

but doing it dynamically...I mean depending on my logic I may send 200 or 301... because the way I'm using the bundle (one method for many types of actions ....), does not respect the principle of REST but I like it to be like this for now.


Solution

  • You can return a view with the status code set directly rather than leaving it for the response listener to handle like..

    use FOS\RestBundle\View\View;
    
    public function deleteUserAction()
    {
        // ... do stuff and generate status code
    
        return View::create(null, $statusCode);
    }