@@ -146,32 +146,25 @@ will be executed. In the next section, you'll learn exactly what that means.
146
146
.. tip ::
147
147
148
148
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
150
150
features of Symfony2, a framework that never imposes you a particular
151
151
configuration format.
152
152
153
153
Controllers
154
154
~~~~~~~~~~~
155
155
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> `
160
160
and :ref: `Response<component-http-foundation-response> `. The simplest possible
161
161
controller might create the response by hand, based on the request::
162
162
163
163
use Symfony\Component\HttpFoundation\Response;
164
164
165
165
$name = $request->query->get('name');
166
166
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);
175
168
176
169
Symfony2 chooses the controller based on the ``_controller `` value from the
177
170
routing configuration: ``AcmeDemoBundle:Welcome:index ``. This string is the
@@ -195,15 +188,15 @@ the ``Acme\DemoBundle\Controller\WelcomeController`` class::
195
188
196
189
You could have used the full class and method name -
197
190
``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.
200
193
201
194
The ``WelcomeController `` class extends the built-in ``Controller `` class,
202
195
which provides useful shortcut methods, like the
203
196
:ref: `render()<controller-rendering-templates> ` method that loads and renders
204
197
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::
207
200
208
201
public function indexAction()
209
202
{
@@ -218,13 +211,6 @@ the ``Response`` object that should be delivered back to the user. This ``Respon
218
211
object can be populated with HTML code, represent a client redirect, or even
219
212
return the contents of a JPG image with a ``Content-Type `` header of ``image/jpg ``.
220
213
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
-
228
214
The template name, ``AcmeDemoBundle:Welcome:index.html.twig ``, is the template
229
215
*logical name * and it references the ``Resources/views/Welcome/index.html.twig ``
230
216
file inside the AcmeDemoBundle (located at ``src/Acme/DemoBundle ``).
242
228
type : annotation
243
229
prefix : /demo
244
230
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
248
233
to the ``src/Acme/DemoBundle/Controller/DemoController.php `` file. In this
249
234
file, routes are defined as annotations on action methods::
250
235
@@ -266,31 +251,19 @@ file, routes are defined as annotations on action methods::
266
251
// ...
267
252
}
268
253
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.
279
258
280
259
If you take a closer look at the controller code, you can see that instead of
281
260
rendering a template and returning a ``Response `` object like before, it
282
261
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
285
264
of the controller. So, in this example, the ``AcmeDemoBundle:Demo:hello.html.twig ``
286
265
template is rendered (located at ``src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig ``).
287
266
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
-
294
267
Templates
295
268
~~~~~~~~~
296
269
0 commit comments