New in Symfony 2.4: Using Constants for HTTP Status Code

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Contributed by
Fabien Potencier
in #8820.

Instead of relying on HTTP status code as integer in your code, which might look like magic values to some, it might be more readable to use the new constants provided by the Symfony Response class in 2.4:

use Symfony\Component\HttpFoundation\Response;

$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());

See the Response class for a full list of supported constants.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

New in Symfony 2.4: Using Constants for HTTP Status Code

Tweet this


Just FYI, this list of constant has been part of FOSRestBundle (and the FOSRest lib which is now deprecated) for a while but its of course much more convenient to have it right in core.
I was wondering why we didn't have this feature before. Little but awesome :)
Small but smart change!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.