Skip to content

Commit 63f671c

Browse files
committed
[quick_tour] updated the "Controllers" section
1 parent 6e5a6a2 commit 63f671c

File tree

1 file changed

+18
-48
lines changed

1 file changed

+18
-48
lines changed

quick_tour/the_big_picture.rst

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,25 @@ will be executed. In the next section, you'll learn exactly what that means.
146146
.. tip::
147147

148148
In addition to YAML format, routes can be configured in XML or PHP files
149-
and even as annotations on PHP classes. This flexibility is one of the main
149+
and even embedded in PHP annotations. This flexibility is one of the main
150150
features of Symfony2, a framework that never imposes you a particular
151151
configuration format.
152152

153153
Controllers
154154
~~~~~~~~~~~
155155

156-
A controller is a fancy name for a PHP function or method that handles incoming
157-
*requests* and returns *responses* (often HTML code). Instead of using the
158-
PHP global variables and functions (like ``$_GET`` or ``header()``) to manage
159-
these HTTP messages, Symfony uses objects: :ref:`Request<component-http-foundation-request>`
156+
A controller is a PHP function or method that handles incoming *requests* and
157+
returns *responses* (often HTML code). Instead of using the PHP global variables
158+
and functions (like ``$_GET`` or ``header()``) to manage these HTTP messages
159+
Symfony uses objects: :ref:`Request<component-http-foundation-request>`
160160
and :ref:`Response<component-http-foundation-response>`. The simplest possible
161161
controller might create the response by hand, based on the request::
162162

163163
use Symfony\Component\HttpFoundation\Response;
164164

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

167-
return new Response('Hello '.$name, Response::HTTP_OK, array('Content-Type' => 'text/plain'));
168-
169-
.. versionadded:: 2.4
170-
Support for HTTP status code constants was added in Symfony 2.4.
171-
172-
.. note::
173-
174-
Symfony2 embraces the HTTP Specification, which are the rules that govern
175-
all communication on the Web. Read the ":doc:`/book/http_fundamentals`"
176-
chapter of the book to learn more about this and the added power that
177-
this brings.
167+
return new Response('Hello '.$name);
178168

179169
Symfony2 chooses the controller based on the ``_controller`` value from the
180170
routing configuration: ``AcmeDemoBundle:Welcome:index``. This string is the
@@ -198,15 +188,15 @@ the ``Acme\DemoBundle\Controller\WelcomeController`` class::
198188

199189
You could have used the full class and method name -
200190
``Acme\DemoBundle\Controller\WelcomeController::indexAction`` - for the
201-
``_controller`` value. But if you follow some simple conventions, the
202-
logical name is shorter and allows for more flexibility.
191+
``_controller`` value. But using the logical name is shorter and allows
192+
for more flexibility.
203193

204194
The ``WelcomeController`` class extends the built-in ``Controller`` class,
205195
which provides useful shortcut methods, like the
206196
:ref:`render()<controller-rendering-templates>` method that loads and renders
207197
a template (``AcmeDemoBundle:Welcome:index.html.twig``). The returned value
208-
is a Response object populated with the rendered content. So, if the need
209-
arises, the Response can be tweaked before it is sent to the browser::
198+
is a ``Response`` object populated with the rendered content. So, if the need
199+
arises, the ``Response`` can be tweaked before it is sent to the browser::
210200

211201
public function indexAction()
212202
{
@@ -221,13 +211,6 @@ the ``Response`` object that should be delivered back to the user. This ``Respon
221211
object can be populated with HTML code, represent a client redirect, or even
222212
return the contents of a JPG image with a ``Content-Type`` header of ``image/jpg``.
223213

224-
.. tip::
225-
226-
Extending the ``Controller`` base class is optional. As a matter of fact,
227-
a controller can be a plain PHP function or even a PHP closure.
228-
":doc:`The Controller</book/controller>`" chapter of the book tells you
229-
everything about Symfony2 controllers.
230-
231214
The template name, ``AcmeDemoBundle:Welcome:index.html.twig``, is the template
232215
*logical name* and it references the ``Resources/views/Welcome/index.html.twig``
233216
file inside the AcmeDemoBundle (located at ``src/Acme/DemoBundle``).
@@ -245,9 +228,8 @@ key:
245228
type: annotation
246229
prefix: /demo
247230
248-
Symfony2 can read/import the routing information from different files written
249-
in YAML, XML, PHP, or even embedded in PHP annotations. Here, the file's
250-
*logical name* is ``@AcmeDemoBundle/Controller/DemoController.php`` and refers
231+
The *logical name* of the file containing the ``_demo`` routes is
232+
``@AcmeDemoBundle/Controller/DemoController.php`` and refers
251233
to the ``src/Acme/DemoBundle/Controller/DemoController.php`` file. In this
252234
file, routes are defined as annotations on action methods::
253235

@@ -269,31 +251,19 @@ file, routes are defined as annotations on action methods::
269251
// ...
270252
}
271253

272-
The ``@Route()`` annotation defines a new route with a path of
273-
``/hello/{name}`` that executes the ``helloAction`` method when matched. A
274-
string enclosed in curly brackets like ``{name}`` is called a placeholder. As
275-
you can see, its value can be retrieved through the ``$name`` method argument.
276-
277-
.. note::
278-
279-
Even if annotations are not natively supported by PHP, you can use them
280-
in Symfony2 as a convenient way to configure the framework behavior and
281-
keep the configuration next to the code.
254+
The ``@Route()`` annotation creates a new route matching the ``/hello/{name}``
255+
path to the ``helloAction()`` method. Any string enclosed in curly brackets,
256+
like ``{name}``, is considered a variable that can be directly retrieved as a
257+
method argument with the same name.
282258

283259
If you take a closer look at the controller code, you can see that instead of
284260
rendering a template and returning a ``Response`` object like before, it
285261
just returns an array of parameters. The ``@Template()`` annotation tells
286-
Symfony to render the template for you, passing in each variable of the array
287-
to the template. The name of the template that's rendered follows the name
262+
Symfony to render the template for you, passing to it each variable of the
263+
returned array. The name of the template that's rendered follows the name
288264
of the controller. So, in this example, the ``AcmeDemoBundle:Demo:hello.html.twig``
289265
template is rendered (located at ``src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig``).
290266

291-
.. tip::
292-
293-
The ``@Route()`` and ``@Template()`` annotations are more powerful than
294-
the simple examples shown in this tutorial. Learn more about "`annotations in controllers`_"
295-
in the official documentation.
296-
297267
Templates
298268
~~~~~~~~~
299269

0 commit comments

Comments
 (0)