@@ -146,35 +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, 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);
178
168
179
169
Symfony2 chooses the controller based on the ``_controller `` value from the
180
170
routing configuration: ``AcmeDemoBundle:Welcome:index ``. This string is the
@@ -198,15 +188,15 @@ the ``Acme\DemoBundle\Controller\WelcomeController`` class::
198
188
199
189
You could have used the full class and method name -
200
190
``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.
203
193
204
194
The ``WelcomeController `` class extends the built-in ``Controller `` class,
205
195
which provides useful shortcut methods, like the
206
196
:ref: `render()<controller-rendering-templates> ` method that loads and renders
207
197
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::
210
200
211
201
public function indexAction()
212
202
{
@@ -221,13 +211,6 @@ the ``Response`` object that should be delivered back to the user. This ``Respon
221
211
object can be populated with HTML code, represent a client redirect, or even
222
212
return the contents of a JPG image with a ``Content-Type `` header of ``image/jpg ``.
223
213
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
-
231
214
The template name, ``AcmeDemoBundle:Welcome:index.html.twig ``, is the template
232
215
*logical name * and it references the ``Resources/views/Welcome/index.html.twig ``
233
216
file inside the AcmeDemoBundle (located at ``src/Acme/DemoBundle ``).
245
228
type : annotation
246
229
prefix : /demo
247
230
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
251
233
to the ``src/Acme/DemoBundle/Controller/DemoController.php `` file. In this
252
234
file, routes are defined as annotations on action methods::
253
235
@@ -269,31 +251,19 @@ file, routes are defined as annotations on action methods::
269
251
// ...
270
252
}
271
253
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.
282
258
283
259
If you take a closer look at the controller code, you can see that instead of
284
260
rendering a template and returning a ``Response `` object like before, it
285
261
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
288
264
of the controller. So, in this example, the ``AcmeDemoBundle:Demo:hello.html.twig ``
289
265
template is rendered (located at ``src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig ``).
290
266
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
-
297
267
Templates
298
268
~~~~~~~~~
299
269
0 commit comments