Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e094853

Browse files
committed
Merge pull request #3148 from xabbuh/http-status-code-constants
use verbose HTTP status code constants
2 parents a516f61 + 9660455 commit e094853

File tree

11 files changed

+62
-20
lines changed

11 files changed

+62
-20
lines changed

book/controller.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ value to each variable.
501501
directly by duplicating the current request. When this
502502
:ref:`sub request <http-kernel-sub-requests>` is executed via the ``http_kernel``
503503
service the ``HttpKernel`` returns a ``Response`` object::
504-
504+
505505
use Symfony\Component\HttpKernel\HttpKernelInterface;
506-
506+
507507
$path = array(
508508
'_controller' => 'AcmeHelloBundle:Hello:fancy',
509509
'name' => $name,
@@ -750,12 +750,15 @@ headers and content that's sent back to the client::
750750
use Symfony\Component\HttpFoundation\Response;
751751

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

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

759+
.. versionadded:: 2.4
760+
Support for HTTP status code constants was added in Symfony 2.4.
761+
759762
.. tip::
760763

761764
The ``headers`` property is a

book/from_flat_php_to_symfony2.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,15 @@ the HTTP response being returned. Use them to improve the blog:
476476
$response = show_action($request->query->get('id'));
477477
} else {
478478
$html = '<html><body><h1>Page Not Found</h1></body></html>';
479-
$response = new Response($html, 404);
479+
$response = new Response($html, Response::HTTP_NOT_FOUND);
480480
}
481481

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

485+
.. versionadded:: 2.4
486+
Support for HTTP status code constants was added in Symfony 2.4.
487+
485488
The controllers are now responsible for returning a ``Response`` object.
486489
To make this easier, you can add a new ``render_template()`` function, which,
487490
incidentally, acts quite a bit like the Symfony2 templating engine:

book/http_cache.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,15 +1059,18 @@ Here is how you can configure the Symfony2 reverse proxy to support the
10591059

10601060
$response = new Response();
10611061
if (!$this->getStore()->purge($request->getUri())) {
1062-
$response->setStatusCode(404, 'Not purged');
1062+
$response->setStatusCode(Response::HTTP_NOT_FOUND, 'Not purged');
10631063
} else {
1064-
$response->setStatusCode(200, 'Purged');
1064+
$response->setStatusCode(Response::HTTP_OK, 'Purged');
10651065
}
10661066

10671067
return $response;
10681068
}
10691069
}
10701070

1071+
.. versionadded:: 2.4
1072+
Support for HTTP status code constants was added in Symfony 2.4.
1073+
10711074
.. caution::
10721075

10731076
You must protect the ``PURGE`` HTTP method somehow to avoid random people

book/http_fundamentals.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,15 @@ interface to construct the response that needs to be returned to the client::
278278
$response = new Response();
279279

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

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

287+
.. versionadded:: 2.4
288+
Support for HTTP status code constants was added in Symfony 2.4.
289+
287290
If Symfony offered nothing else, you would already have a toolkit for easily
288291
accessing request information and an object-oriented interface for creating
289292
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::
364367
// index.php
365368
use Symfony\Component\HttpFoundation\Request;
366369
use Symfony\Component\HttpFoundation\Response;
370+
367371
$request = Request::createFromGlobals();
368372
$path = $request->getPathInfo(); // the URI path being requested
369373

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

book/internals.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,13 @@ and set a new ``Exception`` object, or do nothing::
417417

418418
return new Response(
419419
'Error',
420-
404 // ignored,
421-
array('X-Status-Code' => 200)
420+
Response::HTTP_NOT_FOUND, // ignored
421+
array('X-Status-Code' => Response::HTTP_OK)
422422
);
423423

424+
.. versionadded:: 2.4
425+
Support for HTTP status code constants was added in Symfony 2.4.
426+
424427
.. index::
425428
single: Event Dispatcher
426429

book/testing.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ document::
268268
To get you started faster, here is a list of the most common and
269269
useful test assertions::
270270

271+
use Symfony\Component\HttpFoundation\Response;
272+
273+
// ...
274+
271275
// Assert that there is at least one h2 tag
272276
// with the class "subtitle"
273277
$this->assertGreaterThan(
@@ -295,7 +299,7 @@ document::
295299
$this->assertTrue($client->getResponse()->isNotFound());
296300
// Assert a specific 200 status code
297301
$this->assertEquals(
298-
200,
302+
Response::HTTP_OK,
299303
$client->getResponse()->getStatusCode()
300304
);
301305

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

313+
.. versionadded:: 2.4
314+
Support for HTTP status code constants was added with Symfony 2.4.
315+
309316
.. index::
310317
single: Tests; Client
311318

components/http_foundation/introduction.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,21 @@ code, and an array of HTTP headers::
309309

310310
$response = new Response(
311311
'Content',
312-
200,
312+
Response::HTTP_OK,
313313
array('content-type' => 'text/html')
314314
);
315315

316+
.. versionadded:: 2.4
317+
Support for HTTP status code constants was added in Symfony 2.4.
318+
316319
These information can also be manipulated after the Response object creation::
317320

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

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

323-
$response->setStatusCode(404);
326+
$response->setStatusCode(Response::HTTP_NOT_FOUND);
324327

325328
When setting the ``Content-Type`` of the Response, you can set the charset,
326329
but it is better to set it via the

cookbook/security/custom_authentication_provider.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,21 @@ set an authenticated token in the security context if successful.
153153
154154
// Deny authentication with a '403 Forbidden' HTTP response
155155
$response = new Response();
156-
$response->setStatusCode(403);
156+
$response->setStatusCode(Response::HTTP_FORBIDDEN);
157157
$event->setResponse($response);
158158
159159
}
160160
161161
// By default deny authorization
162162
$response = new Response();
163-
$response->setStatusCode(403);
163+
$response->setStatusCode(Response::HTTP_FORBIDDEN);
164164
$event->setResponse($response);
165165
}
166166
}
167167
168+
.. versionadded:: 2.4
169+
Support for HTTP status code constants was added in Symfony 2.4.
170+
168171
This listener checks the request for the expected `X-WSSE` header, matches
169172
the value returned for the expected WSSE information, creates a token using
170173
that information, and passes the token on to the authentication manager. If

cookbook/service_container/event_listener.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ event is just one of the core kernel events::
4343
$response->setStatusCode($exception->getStatusCode());
4444
$response->headers->replace($exception->getHeaders());
4545
} else {
46-
$response->setStatusCode(500);
46+
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
4747
}
4848

4949
// Send the modified response object to the event
5050
$event->setResponse($response);
5151
}
5252
}
5353

54+
.. versionadded:: 2.4
55+
Support for HTTP status code constants was added in Symfony 2.4.
56+
5457
.. tip::
5558

5659
Each event receives a slightly different type of ``$event`` object. For

cookbook/testing/insulating_clients.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,26 @@ How to test the Interaction of several Clients
77
If you need to simulate an interaction between different Clients (think of a
88
chat for instance), create several Clients::
99

10+
// ...
11+
1012
$harry = static::createClient();
1113
$sally = static::createClient();
1214

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

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

21+
.. versionadded:: 2.4
22+
Support for HTTP status code constants was added in Symfony 2.4.
23+
1924
This works except when your code maintains a global state or if it depends on
2025
a third-party library that has some kind of global state. In such a case, you
2126
can insulate your clients::
2227

28+
// ...
29+
2330
$harry = static::createClient();
2431
$sally = static::createClient();
2532

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

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

3542
Insulated clients transparently execute their requests in a dedicated and

quick_tour/the_big_picture.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ controller might create the response by hand, based on the request::
232232

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

235-
return new Response('Hello '.$name, 200, array('Content-Type' => 'text/plain'));
235+
return new Response('Hello '.$name, Response::HTTP_OK, array('Content-Type' => 'text/plain'));
236+
237+
.. versionadded:: 2.4
238+
Support for HTTP status code constants was added in Symfony 2.4.
236239

237240
.. note::
238241

@@ -421,7 +424,7 @@ When loaded and enabled (by default in the ``dev`` :ref:`environment<quick-tour-
421424
the Profiler provides a web interface for a *huge* amount of information recorded
422425
on each request, including logs, a timeline of the request, GET or POST parameters,
423426
security details, database queries and more!
424-
427+
425428
Of course, it would be unwise to have these tools enabled when you deploy
426429
your application, so by default, the profiler is not enabled in the ``prod``
427430
environment.

0 commit comments

Comments
 (0)