With RESTful APIs, you always use HTTP codes to provide meaningful messages to the client.
To do this, you use a Response
object to send a variety of HTTP codes. Symfony provides a few Exception handling scenarios which will result in 403
, 404
, or 500
errors, but not as extensive as this list. All exceptions will do is throw the same HTTP codes that you could achieve manually, albeit with more meaningful internal debugging errors in the development environment but with less control.
To send a meaningful HTTP error code (such as an object being successfully created):
use Symfony\Component\HttpFoundation\Response;
$response = new Response(
'A custom message or an XML/JSON/anything object',
Response::HTTP_CREATED,
array('content-type' => 'text/html')
// Can also be application/json or application/xml .. list goes on
);
but since you're using the FOSRestBundle already, you should be reading the documentation about the view layer and how to use it instead. The view layer will already handle these HTTP codes for you and provide responses in multiple formats just like a RESTful interface should.