@@ -21,7 +21,7 @@ to render the content of a page.
21
21
A Simple Controller
22
22
-------------------
23
23
24
- While a controller can be any PHP callable (a function, method on an object,
24
+ While a controller can be any PHP callable (function, method on an object,
25
25
or a ``Closure ``), a controller is usually a method inside a controller
26
26
class::
27
27
@@ -46,7 +46,7 @@ class::
46
46
}
47
47
}
48
48
49
- The controller is the ``number() `` method, which lives inside a
49
+ The controller is the ``number() `` method, which lives inside the
50
50
controller class ``LuckyController ``.
51
51
52
52
This controller is pretty straightforward:
@@ -91,9 +91,9 @@ For more information on routing, see :doc:`/routing`.
91
91
The Base Controller Class & Services
92
92
------------------------------------
93
93
94
- To make life nicer , Symfony comes with an optional base controller class called
94
+ To aid development , Symfony comes with an optional base controller class called
95
95
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController `.
96
- You can extend it to get access to some `helper methods `_.
96
+ It can be extended to gain access to `helper methods `_.
97
97
98
98
Add the ``use `` statement atop your controller class and then modify
99
99
``LuckyController `` to extend it:
@@ -354,8 +354,8 @@ The Request object as a Controller Argument
354
354
-------------------------------------------
355
355
356
356
What if you need to read query parameters, grab a request header or get access
357
- to an uploaded file? All of that information is stored in Symfony's ``Request ``
358
- object. To get it in your controller, add it as an argument and
357
+ to an uploaded file? That information is stored in Symfony's ``Request ``
358
+ object. To access it in your controller, add it as an argument and
359
359
**type-hint it with the Request class **::
360
360
361
361
use Symfony\Component\HttpFoundation\Request;
@@ -531,13 +531,13 @@ the ``Request`` class::
531
531
The ``Request `` class has several public properties and methods that return any
532
532
information you need about the request.
533
533
534
- Like the ``Request ``, the ``Response `` object has also a public ``headers `` property.
535
- This is a :class: `Symfony\\ Component\\ HttpFoundation\\ ResponseHeaderBag ` that has
536
- some nice methods for getting and setting response headers. The header names are
537
- normalized so that using ``Content-Type `` is equivalent to `` content-type `` or even
538
- ``content_type ``.
534
+ Like the ``Request ``, the ``Response `` object has a public ``headers `` property.
535
+ This object is of the type :class: `Symfony\\ Component\\ HttpFoundation\\ ResponseHeaderBag `
536
+ and provides methods for getting and setting response headers. The header names are
537
+ normalized. As a result, the name ``Content-Type `` is equivalent to
538
+ the name `` content-type `` or ``content_type ``.
539
539
540
- The only requirement for a controller is to return a ``Response `` object::
540
+ In Symfony, a controller is required to return a ``Response `` object::
541
541
542
542
use Symfony\Component\HttpFoundation\Response;
543
543
@@ -548,15 +548,16 @@ The only requirement for a controller is to return a ``Response`` object::
548
548
$response = new Response('<style> ... </style>');
549
549
$response->headers->set('Content-Type', 'text/css');
550
550
551
- There are special classes that make certain kinds of responses easier. Some of these
552
- are mentioned below. To learn more about the ``Request `` and ``Response `` (and special
553
- ``Response `` classes), see the :ref: `HttpFoundation component documentation <component-http-foundation-request >`.
551
+ To facilitate this, different response objects are included to address different
552
+ response types. Some of these are mentioned below. To learn more about the
553
+ ``Request `` and ``Response `` (and different ``Response `` classes), see the
554
+ :ref: `HttpFoundation component documentation <component-http-foundation-request >`.
554
555
555
556
Returning JSON Response
556
557
~~~~~~~~~~~~~~~~~~~~~~~
557
558
558
559
To return JSON from a controller, use the ``json() `` helper method. This returns a
559
- special ``JsonResponse `` object that encodes the data automatically::
560
+ ``JsonResponse `` object that encodes the data automatically::
560
561
561
562
// ...
562
563
public function index()
@@ -606,13 +607,16 @@ The ``file()`` helper provides some arguments to configure its behavior::
606
607
Final Thoughts
607
608
--------------
608
609
609
- Whenever you create a page, you'll ultimately need to write some code that
610
- contains the logic for that page. In Symfony, this is called a controller,
611
- and it's a PHP function where you can do anything in order to return the
612
- final ``Response `` object that will be returned to the user.
610
+ In Symfony, a controller is usually a class method which is used to accept
611
+ requests, and return a ``Response `` object. When mapped with a URL, a controller
612
+ becomes accessible and its response can be viewed.
613
613
614
- To make life easier, you'll probably extend the base ``AbstractController `` class because
615
- this gives access to shortcut methods (like ``render() `` and ``redirectToRoute() ``).
614
+ To facilitate the development of controllers, Symfony provides an
615
+ ``AbstractController ``. It can be used to extend the controller class allowing
616
+ access to some frequently used utilities such as ``render() `` and
617
+ ``redirectToRoute() ``. The ``AbstractController `` also provides the
618
+ ``createNotFoundException() `` utility which is used to return a page not found
619
+ response.
616
620
617
621
In other articles, you'll learn how to use specific services from inside your controller
618
622
that will help you persist and fetch objects from a database, process form submissions,
0 commit comments