@@ -15,12 +15,11 @@ simple two-step process:
15
15
16
16
#. *Create a controller *: A controller is the function you write that builds
17
17
the page. You take the incoming request information and use it to create
18
- a Symfony ``Response ``, which could hold HTML content, a JSON string or
19
- anything else.
18
+ a Symfony ``Response `` object , which can hold HTML content, a JSON string
19
+ or anything else.
20
20
21
21
Just like on the web, every interaction is initiated by an HTTP request.
22
- Your job is pure and simple: understand that request and return the appropriate
23
- HTTP response.
22
+ Your job is pure and simple: understand that request and return a response.
24
23
25
24
.. index ::
26
25
single: Page creation; Example
@@ -59,7 +58,7 @@ a method inside of it that will be executed when someone goes to ``/lucky/random
59
58
}
60
59
}
61
60
62
- Before explaining this, try it out!
61
+ Before diving into this, test it out!
63
62
64
63
.. code-block :: text
65
64
@@ -71,15 +70,15 @@ Before explaining this, try it out!
71
70
replace ``http://localhost:8000 `` with your host name - like
72
71
``http://symfony.local/app_dev.php/lucky/number ``.
73
72
74
- If you see a lucky number being printed back to you, congratulations!
73
+ If you see a lucky number being printed back to you, congratulations! But
74
+ before you run off to play the lottery, check out how this works.
75
75
76
76
The ``@Route `` above ``numberAction() `` is called an *annotation * and it
77
77
defines the URL pattern. You can also write routes in YAML (or other formats):
78
- read about this in the :doc: `routing </book/routing >` chapter. In fact, most
79
- routing example in the documentation will have tabs to show you how each
80
- format looks.
78
+ read about this in the :doc: `routing </book/routing >` chapter. Actually, most
79
+ routing examples in the docs have tabs that show you how each format looks.
81
80
82
- The method below the annotation - ``numberAction `` - is called the *contrller *
81
+ The method below the annotation - ``numberAction `` - is called the *controller *
83
82
and is where you build the page. The only rule is that a controller *must *
84
83
return a Symfony :ref: `Response <component-http-foundation-response >` object
85
84
(and you'll even learn to bend this rule eventually).
@@ -90,8 +89,8 @@ return a Symfony :ref:`Response <component-http-foundation-response>` object
90
89
91
90
Great question! By including ``app_dev.php `` in the URL, you're executing
92
91
Symfony through a file - ``web/app_dev.php `` - that boots it in the ``dev ``
93
- environment. This enables great debugging tools and rebuilds any cached
94
- files automatically. For production, you'll use clean URLS - like
92
+ environment. This enables great debugging tools and rebuilds cached
93
+ files automatically. For production, you'll use clean URLs - like
95
94
``http://localhost:8000/lucky/number `` - that execute a different file -
96
95
``app.php `` - that's optimized for speed. To learn more about this and
97
96
environments, see :ref: `book-page-creation-prod-cache-clear `.
@@ -100,10 +99,11 @@ Creating a JSON Response
100
99
~~~~~~~~~~~~~~~~~~~~~~~~
101
100
102
101
The ``Response `` object you return in your controller can contain HTML, JSON
103
- or anything you want. And you can easily set HTTP headers or the status code.
102
+ or even a binary file like an image or PDF. You can easily set HTTP headers
103
+ or the status code.
104
104
105
- Suppose you want to create a JSON endpoint that returns a lucky number. Just
106
- add a second method to ``LuckyController ``::
105
+ Suppose you want to create a JSON endpoint that returns the lucky number.
106
+ Just add a second method to ``LuckyController ``::
107
107
108
108
// src/AppBundle/Controller/LuckyController.php
109
109
// ...
@@ -164,10 +164,10 @@ You can even shorten this with the handy :class:`Symfony\\Component\\HttpFoundat
164
164
Dynamic URL Patterns: /lucky/number/{count}
165
165
-------------------------------------------
166
166
167
- You 're doing great! But Symfony's routing can do a lot more. Suppose now
168
- that you want a user to be able to go to ``/lucky/number/5 `` in order to
169
- generate *5 * lucky numbers at once. Update the route to have a ``{wildcard} ``
170
- part at the end:
167
+ Woh, you 're doing great! But Symfony's routing can do a lot more. Suppose
168
+ now that you want a user to be able to go to ``/lucky/number/5 `` to generate
169
+ *5 * lucky numbers at once. Update the route to have a ``{wildcard} `` part
170
+ at the end:
171
171
172
172
.. configuration-block ::
173
173
@@ -258,10 +258,14 @@ Try it by going to ``/lucky/number/XX`` - replacing XX with *any* number:
258
258
259
259
http://localhost:8000/app_dev.php/lucky/number/7
260
260
261
- You should see *7 * lucky numbers printed out! The routing system can do a
262
- *lot * more, like supporting multiple placeholders (e.g. ``/blog/{category}/{page}) ``),
263
- making placeholders optional and forcing placeholder to match a regular expression
264
- (e.g. so that ``{count} `` *must * be a number).
261
+ You should see *7 * lucky numbers printed out! You can get the value of any
262
+ ``{placeholder} `` in your route by adding a ``$placeholder `` argument to
263
+ your controller. Just make sure they have the same name.
264
+
265
+ The routing system can do a *lot * more, like supporting multiple placeholders
266
+ (e.g. ``/blog/{category}/{page}) ``), making placeholders optional and forcing
267
+ placeholder to match a regular expression (e.g. so that ``{count} `` *must *
268
+ be a number).
265
269
266
270
Find out about all of this and become a routing expert in the
267
271
:doc: `Routing </book/routing >` chapter.
@@ -271,10 +275,10 @@ Rendering a Template (with the Service Container)
271
275
272
276
If you're returning HTML from your controller, you'll probably want to render
273
277
a template. Fortunately, Symfony comes with Twig: a templating language that's
274
- easy to learn , powerful and fun to use .
278
+ easy, powerful and actually quite fun .
275
279
276
- So far, ``LuckyController `` doesn't extend any base class. In order to use
277
- Twig - or many other tools in Symfony - you'll need to extend Symfony's base
280
+ So far, ``LuckyController `` doesn't extend any base class. The easiest way
281
+ to use Twig - or many other tools in Symfony - is to extend Symfony's base
278
282
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class::
279
283
280
284
// src/AppBundle/Controller/LuckyController.php
@@ -309,10 +313,7 @@ To render a Twig template, use a service called ``templating``::
309
313
*/
310
314
public function numberAction($count)
311
315
{
312
- $numbers = array();
313
- for ($i = 0; $i < $count; $i++) {
314
- $numbers[] = rand(0, 100);
315
- }
316
+ // ...
316
317
$numbersList = implode(', ', $numbers);
317
318
318
319
$html = $this->container->get('templating')->render(
@@ -328,12 +329,12 @@ To render a Twig template, use a service called ``templating``::
328
329
329
330
You'll learn a lot more about the important "service container" as you keep
330
331
reading. For now, you just need to know that it holds a lot of objects, and
331
- you can " get" any object by using its nickname, like ``templating `` or `` logger ``.
332
- The ``templating `` service is an instance of :class: `Symfony\\ Bundle\\ TwigBundle\\ TwigEngine `
332
+ you can `` get() `` any object by using its nickname, like ``templating `` or
333
+ `` logger ``. The ``templating `` service is an instance of :class: `Symfony\\ Bundle\\ TwigBundle\\ TwigEngine `
333
334
and this has a ``render() `` method.
334
335
335
- And by extending the ``Controller `` class, you also get a lot of shortcut
336
- methods, like ``render() ``. Use it to shorten the code ::
336
+ But this can get even easier! By extending the ``Controller `` class, you
337
+ also get a lot of shortcut methods, like ``render() ``::
337
338
338
339
// src/AppBundle/Controller/LuckyController.php
339
340
// ...
@@ -343,6 +344,8 @@ methods, like ``render()``. Use it to shorten the code::
343
344
*/
344
345
public function numberAction($count)
345
346
{
347
+ // ...
348
+
346
349
/*
347
350
$html = $this->container->get('templating')->render(
348
351
'lucky/number.html.twig',
@@ -362,6 +365,11 @@ methods, like ``render()``. Use it to shorten the code::
362
365
Learn more about these shortcut methods and how they work in the
363
366
:doc: `Controller </book/controller >` chapter.
364
367
368
+ .. tip ::
369
+
370
+ For more advanced users, you can also
371
+ :doc: `register your controllers as services </cookbook/controller/service >`.
372
+
365
373
Create the Template
366
374
~~~~~~~~~~~~~~~~~~~
367
375
@@ -394,7 +402,8 @@ a ``number.html.twig`` file inside of it:
394
402
395
403
Welcome to Twig! This simple file already shows off the basics: like how
396
404
the ``{{ variableName }} `` syntax is used to print something. The ``luckyNumberList ``
397
- is a variable that you're passing into the template from the controller.
405
+ is a variable that you're passing into the template from the ``render `` call
406
+ in your controller.
398
407
399
408
The ``{% extends 'base.html.twig' %} `` points to a layout file that lives
400
409
at `app/Resources/views/base.html.twig `_ and came with your new project.
@@ -412,17 +421,17 @@ If you view the source code, you now have a basic HTML structure thanks to
412
421
``base.html.twig ``.
413
422
414
423
This is just the surface of Twig's power. When you're ready to master its
415
- syntax, loop over arrays, render other templates or other cool things, read
424
+ syntax, loop over arrays, render other templates and other cool things, read
416
425
the :doc: `Templating </book/templating >` chapter.
417
426
418
427
Exploring the Project
419
428
---------------------
420
429
421
- You've already created a flexible URL, rendered template that uses inheritance
430
+ You've already created a flexible URL, rendered a template that uses inheritance
422
431
and created a JSON endpoint. Nice!
423
432
424
- Now, take a minute to explore the files in your project. You've already worked
425
- inside the two most important directories:
433
+ It's time to explore and demystify the files in your project. You've already
434
+ worked inside the two most important directories:
426
435
427
436
``app/ ``
428
437
Contains things like configuration and templates. Basically, anything
@@ -445,14 +454,14 @@ and everything lives inside of it. A bundle is like a "plugin" and you can
445
454
`find open source bundles `_ and install them into your project. But even
446
455
*your * code lives in a bundle - typically ``AppBundle `` (though there's
447
456
nothing special about ``AppBundle ``). To find out more about bundles and
448
- when you might create new ones (hint: sharing code between projects), see
449
- the :doc: `Bundles </book/bundles >` chapter.
457
+ why you might create multiple bundles (hint: sharing code between projects),
458
+ see the :doc: `Bundles </book/bundles >` chapter.
450
459
451
460
So what about the other directories in the project?
452
461
453
462
``vendor/ ``
454
463
Vendor (i.e. third-party) libraries and bundles are downloaded here by
455
- `Composer `_ package manager.
464
+ the `Composer `_ package manager.
456
465
457
466
``web/ ``
458
467
This is the document root for the project and contains any publicly accessible
@@ -461,8 +470,8 @@ So what about the other directories in the project?
461
470
462
471
.. seealso ::
463
472
464
- If you need to, you can override the default directory structure.
465
- See :doc: `/cookbook/configuration/override_dir_structure `.
473
+ Symfony is flexible. If you need to, you can easily override the default
474
+ directory structure. See :doc: `/cookbook/configuration/override_dir_structure `.
466
475
467
476
Application Configuration
468
477
-------------------------
@@ -558,8 +567,10 @@ environment, imports and parameters. To learn all of it, see the
558
567
What's Next?
559
568
------------
560
569
561
- If you've made it this far, you are already starting to master Symfony.
562
- Now, finish mastering the fundamentals by reading these chapters:
570
+ Congrats! You're already starting to master Symfony and learn a whole new
571
+ way of building beautiful, functional, fast and maintainable apps.
572
+
573
+ Ok, time to finish mastering the fundamentals by reading these chapters:
563
574
564
575
* :doc: `/book/controller `
565
576
* :doc: `/book/routing `
@@ -569,8 +580,8 @@ Then, learn about Symfony's :doc:`service container </book/service_container>`
569
580
the :doc: `form system </book/forms >`, using :doc: `Doctrine </book/doctrine >`
570
581
if you need to query a database and more. in the :doc: `Symfony Book </book/index >`.
571
582
572
- There's also a :doc: `Cookbook </cookbook/index >` packed with more advanced
573
- "how to" articles to solve *a lot * of common problems.
583
+ There's also a :doc: `Cookbook </cookbook/index >` * packed * with more advanced
584
+ "how to" articles to solve *a lot * of problems.
574
585
575
586
Have fun!
576
587
0 commit comments