Skip to content

Commit 20e9fb0

Browse files
javiereguiluzweaverryan
authored andcommitted
[quick_tour] updated the "Controllers" section
1 parent 29992cd commit 20e9fb0

File tree

1 file changed

+18
-45
lines changed

1 file changed

+18
-45
lines changed

quick_tour/the_big_picture.rst

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -146,32 +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, 200, array('Content-Type' => 'text/plain'));
168-
169-
.. note::
170-
171-
Symfony2 embraces the HTTP Specification, which are the rules that govern
172-
all communication on the Web. Read the ":doc:`/book/http_fundamentals`"
173-
chapter of the book to learn more about this and the added power that
174-
this brings.
167+
return new Response('Hello '.$name);
175168

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

196189
You could have used the full class and method name -
197190
``Acme\DemoBundle\Controller\WelcomeController::indexAction`` - for the
198-
``_controller`` value. But if you follow some simple conventions, the
199-
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.
200193

201194
The ``WelcomeController`` class extends the built-in ``Controller`` class,
202195
which provides useful shortcut methods, like the
203196
:ref:`render()<controller-rendering-templates>` method that loads and renders
204197
a template (``AcmeDemoBundle:Welcome:index.html.twig``). The returned value
205-
is a Response object populated with the rendered content. So, if the need
206-
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::
207200

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

221-
.. tip::
222-
223-
Extending the ``Controller`` base class is optional. As a matter of fact,
224-
a controller can be a plain PHP function or even a PHP closure.
225-
":doc:`The Controller</book/controller>`" chapter of the book tells you
226-
everything about Symfony2 controllers.
227-
228214
The template name, ``AcmeDemoBundle:Welcome:index.html.twig``, is the template
229215
*logical name* and it references the ``Resources/views/Welcome/index.html.twig``
230216
file inside the AcmeDemoBundle (located at ``src/Acme/DemoBundle``).
@@ -242,9 +228,8 @@ key:
242228
type: annotation
243229
prefix: /demo
244230
245-
Symfony2 can read/import the routing information from different files written
246-
in YAML, XML, PHP, or even embedded in PHP annotations. Here, the file's
247-
*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
248233
to the ``src/Acme/DemoBundle/Controller/DemoController.php`` file. In this
249234
file, routes are defined as annotations on action methods::
250235

@@ -266,31 +251,19 @@ file, routes are defined as annotations on action methods::
266251
// ...
267252
}
268253

269-
The ``@Route()`` annotation defines a new route with a path of
270-
``/hello/{name}`` that executes the ``helloAction`` method when matched. A
271-
string enclosed in curly brackets like ``{name}`` is called a placeholder. As
272-
you can see, its value can be retrieved through the ``$name`` method argument.
273-
274-
.. note::
275-
276-
Even if annotations are not natively supported by PHP, you can use them
277-
in Symfony2 as a convenient way to configure the framework behavior and
278-
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.
279258

280259
If you take a closer look at the controller code, you can see that instead of
281260
rendering a template and returning a ``Response`` object like before, it
282261
just returns an array of parameters. The ``@Template()`` annotation tells
283-
Symfony to render the template for you, passing in each variable of the array
284-
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
285264
of the controller. So, in this example, the ``AcmeDemoBundle:Demo:hello.html.twig``
286265
template is rendered (located at ``src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig``).
287266

288-
.. tip::
289-
290-
The ``@Route()`` and ``@Template()`` annotations are more powerful than
291-
the simple examples shown in this tutorial. Learn more about "`annotations in controllers`_"
292-
in the official documentation.
293-
294267
Templates
295268
~~~~~~~~~
296269

0 commit comments

Comments
 (0)