Skip to content

use verbose HTTP status code constants #3148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ value to each variable.
directly by duplicating the current request. When this
:ref:`sub request <http-kernel-sub-requests>` is executed via the ``http_kernel``
service the ``HttpKernel`` returns a ``Response`` object::

use Symfony\Component\HttpKernel\HttpKernelInterface;

$path = array(
'_controller' => 'AcmeHelloBundle:Hello:fancy',
'name' => $name,
Expand Down Expand Up @@ -750,12 +750,15 @@ headers and content that's sent back to the client::
use Symfony\Component\HttpFoundation\Response;

// create a simple Response with a 200 status code (the default)
$response = new Response('Hello '.$name, 200);
$response = new Response('Hello '.$name, Response::HTTP_OK);

// create a JSON-response with a 200 status code
$response = new Response(json_encode(array('name' => $name)));
$response->headers->set('Content-Type', 'application/json');

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

.. tip::

The ``headers`` property is a
Expand Down
5 changes: 4 additions & 1 deletion book/from_flat_php_to_symfony2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,15 @@ the HTTP response being returned. Use them to improve the blog:
$response = show_action($request->query->get('id'));
} else {
$html = '<html><body><h1>Page Not Found</h1></body></html>';
$response = new Response($html, 404);
$response = new Response($html, Response::HTTP_NOT_FOUND);
}

// echo the headers and send the response
$response->send();

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

The controllers are now responsible for returning a ``Response`` object.
To make this easier, you can add a new ``render_template()`` function, which,
incidentally, acts quite a bit like the Symfony2 templating engine:
Expand Down
7 changes: 5 additions & 2 deletions book/http_cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1059,15 +1059,18 @@ Here is how you can configure the Symfony2 reverse proxy to support the

$response = new Response();
if (!$this->getStore()->purge($request->getUri())) {
$response->setStatusCode(404, 'Not purged');
$response->setStatusCode(Response::HTTP_NOT_FOUND, 'Not purged');
} else {
$response->setStatusCode(200, 'Purged');
$response->setStatusCode(Response::HTTP_OK, 'Purged');
}

return $response;
}
}

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

.. caution::

You must protect the ``PURGE`` HTTP method somehow to avoid random people
Expand Down
8 changes: 6 additions & 2 deletions book/http_fundamentals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,15 @@ interface to construct the response that needs to be returned to the client::
$response = new Response();

$response->setContent('<html><body><h1>Hello world!</h1></body></html>');
$response->setStatusCode(200);
$response->setStatusCode(Response::HTTP_OK);
$response->headers->set('Content-Type', 'text/html');

// prints the HTTP headers followed by the content
$response->send();

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

If Symfony offered nothing else, you would already have a toolkit for easily
accessing request information and an object-oriented interface for creating
the response. Even as you learn the many powerful features in Symfony, keep
Expand Down Expand Up @@ -364,6 +367,7 @@ on that value. This can get ugly quickly::
// index.php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$request = Request::createFromGlobals();
$path = $request->getPathInfo(); // the URI path being requested

Expand All @@ -372,7 +376,7 @@ on that value. This can get ugly quickly::
} elseif ($path == '/contact') {
$response = new Response('Contact us');
} else {
$response = new Response('Page not found.', 404);
$response = new Response('Page not found.', Response::HTTP_NOT_FOUND);
}
$response->send();

Expand Down
7 changes: 5 additions & 2 deletions book/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,13 @@ and set a new ``Exception`` object, or do nothing::

return new Response(
'Error',
404 // ignored,
array('X-Status-Code' => 200)
Response::HTTP_NOT_FOUND, // ignored
array('X-Status-Code' => Response::HTTP_OK)
);

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

.. index::
single: Event Dispatcher

Expand Down
9 changes: 8 additions & 1 deletion book/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ document::
To get you started faster, here is a list of the most common and
useful test assertions::

use Symfony\Component\HttpFoundation\Response;

// ...

// Assert that there is at least one h2 tag
// with the class "subtitle"
$this->assertGreaterThan(
Expand Down Expand Up @@ -295,7 +299,7 @@ document::
$this->assertTrue($client->getResponse()->isNotFound());
// Assert a specific 200 status code
$this->assertEquals(
200,
Response::HTTP_OK,
$client->getResponse()->getStatusCode()
);

Expand All @@ -306,6 +310,9 @@ document::
// or simply check that the response is a redirect to any URL
$this->assertTrue($client->getResponse()->isRedirect());

.. versionadded:: 2.4
Support for HTTP status code constants was added with Symfony 2.4.

.. index::
single: Tests; Client

Expand Down
7 changes: 5 additions & 2 deletions components/http_foundation/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,21 @@ code, and an array of HTTP headers::

$response = new Response(
'Content',
200,
Response::HTTP_OK,
array('content-type' => 'text/html')
);

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

These information can also be manipulated after the Response object creation::

$response->setContent('Hello World');

// the headers public attribute is a ResponseHeaderBag
$response->headers->set('Content-Type', 'text/plain');

$response->setStatusCode(404);
$response->setStatusCode(Response::HTTP_NOT_FOUND);

When setting the ``Content-Type`` of the Response, you can set the charset,
but it is better to set it via the
Expand Down
7 changes: 5 additions & 2 deletions cookbook/security/custom_authentication_provider.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,21 @@ set an authenticated token in the security context if successful.

// Deny authentication with a '403 Forbidden' HTTP response
$response = new Response();
$response->setStatusCode(403);
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$event->setResponse($response);

}

// By default deny authorization
$response = new Response();
$response->setStatusCode(403);
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$event->setResponse($response);
}
}

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

This listener checks the request for the expected `X-WSSE` header, matches
the value returned for the expected WSSE information, creates a token using
that information, and passes the token on to the authentication manager. If
Expand Down
5 changes: 4 additions & 1 deletion cookbook/service_container/event_listener.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ event is just one of the core kernel events::
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(500);
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}

// Send the modified response object to the event
$event->setResponse($response);
}
}

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

.. tip::

Each event receives a slightly different type of ``$event`` object. For
Expand Down
11 changes: 9 additions & 2 deletions cookbook/testing/insulating_clients.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ How to test the Interaction of several Clients
If you need to simulate an interaction between different Clients (think of a
chat for instance), create several Clients::

// ...

$harry = static::createClient();
$sally = static::createClient();

$harry->request('POST', '/say/sally/Hello');
$sally->request('GET', '/messages');

$this->assertEquals(201, $harry->getResponse()->getStatusCode());
$this->assertEquals(Response::HTTP_CREATED, $harry->getResponse()->getStatusCode());
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

This works except when your code maintains a global state or if it depends on
a third-party library that has some kind of global state. In such a case, you
can insulate your clients::

// ...

$harry = static::createClient();
$sally = static::createClient();

Expand All @@ -29,7 +36,7 @@ can insulate your clients::
$harry->request('POST', '/say/sally/Hello');
$sally->request('GET', '/messages');

$this->assertEquals(201, $harry->getResponse()->getStatusCode());
$this->assertEquals(Response::HTTP_CREATED, $harry->getResponse()->getStatusCode());
$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just merged this, but now I think we should remove these from this entry and the book/testing entry. The reason is simply clarity - when we're talking about testing, we're not doing anything with Symfony's Response class, so to start using it for the status code constant seems a little bit confusing. Plus, I think it's useful to show people the exact status codes we're testing for. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is much that problem since we don't test the HttpFoundation component. Therefore, I see no problem in relying on the status code constants. But, if you think it's clearer to the raw status code, I won't object in reverting this. I think it's more a matter of taste. :)

If we want to revert this, we must keep an eye on the book/testing.rst file, too. Then, would you like to do that or do you want me to create a separate pull request?


Insulated clients transparently execute their requests in a dedicated and
Expand Down
7 changes: 5 additions & 2 deletions quick_tour/the_big_picture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ controller might create the response by hand, based on the request::

$name = $request->query->get('name');

return new Response('Hello '.$name, 200, array('Content-Type' => 'text/plain'));
return new Response('Hello '.$name, Response::HTTP_OK, array('Content-Type' => 'text/plain'));

.. versionadded:: 2.4
Support for HTTP status code constants was added in Symfony 2.4.

.. note::

Expand Down Expand Up @@ -421,7 +424,7 @@ When loaded and enabled (by default in the ``dev`` :ref:`environment<quick-tour-
the Profiler provides a web interface for a *huge* amount of information recorded
on each request, including logs, a timeline of the request, GET or POST parameters,
security details, database queries and more!

Of course, it would be unwise to have these tools enabled when you deploy
your application, so by default, the profiler is not enabled in the ``prod``
environment.
Expand Down