From 966045557ed7bc3517aa0f55efd0e152c04d20d7 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 3 Nov 2013 15:07:33 +0100 Subject: [PATCH] use verbose HTTP status code constants --- book/controller.rst | 9 ++++++--- book/from_flat_php_to_symfony2.rst | 5 ++++- book/http_cache.rst | 7 +++++-- book/http_fundamentals.rst | 8 ++++++-- book/internals.rst | 7 +++++-- book/testing.rst | 9 ++++++++- components/http_foundation/introduction.rst | 7 +++++-- cookbook/security/custom_authentication_provider.rst | 7 +++++-- cookbook/service_container/event_listener.rst | 5 ++++- cookbook/testing/insulating_clients.rst | 11 +++++++++-- quick_tour/the_big_picture.rst | 7 +++++-- 11 files changed, 62 insertions(+), 20 deletions(-) diff --git a/book/controller.rst b/book/controller.rst index 7907b70b919..a3ca2610273 100755 --- a/book/controller.rst +++ b/book/controller.rst @@ -501,9 +501,9 @@ value to each variable. directly by duplicating the current request. When this :ref:`sub request ` 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, @@ -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 diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index 3590fe44353..76b5076f3cb 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -476,12 +476,15 @@ the HTTP response being returned. Use them to improve the blog: $response = show_action($request->query->get('id')); } else { $html = '

Page Not Found

'; - $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: diff --git a/book/http_cache.rst b/book/http_cache.rst index c1c0b737bea..ab845d1bd85 100644 --- a/book/http_cache.rst +++ b/book/http_cache.rst @@ -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 diff --git a/book/http_fundamentals.rst b/book/http_fundamentals.rst index 902a5065b9e..391f549a7d7 100644 --- a/book/http_fundamentals.rst +++ b/book/http_fundamentals.rst @@ -278,12 +278,15 @@ interface to construct the response that needs to be returned to the client:: $response = new Response(); $response->setContent('

Hello world!

'); - $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 @@ -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 @@ -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(); diff --git a/book/internals.rst b/book/internals.rst index 69078e403e1..33a14abb2c9 100644 --- a/book/internals.rst +++ b/book/internals.rst @@ -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 diff --git a/book/testing.rst b/book/testing.rst index 4deb4229075..34e206f87b0 100644 --- a/book/testing.rst +++ b/book/testing.rst @@ -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( @@ -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() ); @@ -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 diff --git a/components/http_foundation/introduction.rst b/components/http_foundation/introduction.rst index f7cb342037e..185b5366fbb 100644 --- a/components/http_foundation/introduction.rst +++ b/components/http_foundation/introduction.rst @@ -309,10 +309,13 @@ 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'); @@ -320,7 +323,7 @@ These information can also be manipulated after the Response object creation:: // 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 diff --git a/cookbook/security/custom_authentication_provider.rst b/cookbook/security/custom_authentication_provider.rst index 26e3219c062..729c03b5165 100644 --- a/cookbook/security/custom_authentication_provider.rst +++ b/cookbook/security/custom_authentication_provider.rst @@ -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 diff --git a/cookbook/service_container/event_listener.rst b/cookbook/service_container/event_listener.rst index 7e0b5410f79..91f20364bee 100644 --- a/cookbook/service_container/event_listener.rst +++ b/cookbook/service_container/event_listener.rst @@ -43,7 +43,7 @@ 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 @@ -51,6 +51,9 @@ event is just one of the core kernel events:: } } +.. 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 diff --git a/cookbook/testing/insulating_clients.rst b/cookbook/testing/insulating_clients.rst index 3411968eb80..f9c602dcf67 100644 --- a/cookbook/testing/insulating_clients.rst +++ b/cookbook/testing/insulating_clients.rst @@ -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(); @@ -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()); Insulated clients transparently execute their requests in a dedicated and diff --git a/quick_tour/the_big_picture.rst b/quick_tour/the_big_picture.rst index 9ba1161c7a6..8436651ba33 100644 --- a/quick_tour/the_big_picture.rst +++ b/quick_tour/the_big_picture.rst @@ -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:: @@ -421,7 +424,7 @@ When loaded and enabled (by default in the ``dev`` :ref:`environment