@@ -15,12 +15,11 @@ simple two-step process:
1515
1616#. *Create a controller *: A controller is the function you write that builds
1717 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.
2020
2121Just 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.
2423
2524.. index ::
2625 single: Page creation; Example
@@ -59,7 +58,7 @@ a method inside of it that will be executed when someone goes to ``/lucky/random
5958 }
6059 }
6160
62- Before explaining this, try it out!
61+ Before diving into this, test it out!
6362
6463.. code-block :: text
6564
@@ -71,15 +70,15 @@ Before explaining this, try it out!
7170 replace ``http://localhost:8000 `` with your host name - like
7271 ``http://symfony.local/app_dev.php/lucky/number ``.
7372
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.
7575
7676The ``@Route `` above ``numberAction() `` is called an *annotation * and it
7777defines 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.
8180
82- The method below the annotation - ``numberAction `` - is called the *contrller *
81+ The method below the annotation - ``numberAction `` - is called the *controller *
8382and is where you build the page. The only rule is that a controller *must *
8483return a Symfony :ref: `Response <component-http-foundation-response >` object
8584(and you'll even learn to bend this rule eventually).
@@ -90,8 +89,8 @@ return a Symfony :ref:`Response <component-http-foundation-response>` object
9089
9190 Great question! By including ``app_dev.php `` in the URL, you're executing
9291 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
9594 ``http://localhost:8000/lucky/number `` - that execute a different file -
9695 ``app.php `` - that's optimized for speed. To learn more about this and
9796 environments, see :ref: `book-page-creation-prod-cache-clear `.
@@ -100,10 +99,11 @@ Creating a JSON Response
10099~~~~~~~~~~~~~~~~~~~~~~~~
101100
102101The ``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.
104104
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 ``::
107107
108108 // src/AppBundle/Controller/LuckyController.php
109109 // ...
@@ -164,10 +164,10 @@ You can even shorten this with the handy :class:`Symfony\\Component\\HttpFoundat
164164Dynamic URL Patterns: /lucky/number/{count}
165165-------------------------------------------
166166
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:
171171
172172.. configuration-block ::
173173
@@ -258,10 +258,14 @@ Try it by going to ``/lucky/number/XX`` - replacing XX with *any* number:
258258
259259 http://localhost:8000/app_dev.php/lucky/number/7
260260
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).
265269
266270Find out about all of this and become a routing expert in the
267271:doc: `Routing </book/routing >` chapter.
@@ -271,10 +275,10 @@ Rendering a Template (with the Service Container)
271275
272276If you're returning HTML from your controller, you'll probably want to render
273277a 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 .
275279
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
278282:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class::
279283
280284 // src/AppBundle/Controller/LuckyController.php
@@ -309,10 +313,7 @@ To render a Twig template, use a service called ``templating``::
309313 */
310314 public function numberAction($count)
311315 {
312- $numbers = array();
313- for ($i = 0; $i < $count; $i++) {
314- $numbers[] = rand(0, 100);
315- }
316+ // ...
316317 $numbersList = implode(', ', $numbers);
317318
318319 $html = $this->container->get('templating')->render(
@@ -328,12 +329,12 @@ To render a Twig template, use a service called ``templating``::
328329
329330You'll learn a lot more about the important "service container" as you keep
330331reading. 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 `
333334and this has a ``render() `` method.
334335
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() ``::
337338
338339 // src/AppBundle/Controller/LuckyController.php
339340 // ...
@@ -343,6 +344,8 @@ methods, like ``render()``. Use it to shorten the code::
343344 */
344345 public function numberAction($count)
345346 {
347+ // ...
348+
346349 /*
347350 $html = $this->container->get('templating')->render(
348351 'lucky/number.html.twig',
@@ -362,6 +365,11 @@ methods, like ``render()``. Use it to shorten the code::
362365Learn more about these shortcut methods and how they work in the
363366:doc: `Controller </book/controller >` chapter.
364367
368+ .. tip ::
369+
370+ For more advanced users, you can also
371+ :doc: `register your controllers as services </cookbook/controller/service >`.
372+
365373Create the Template
366374~~~~~~~~~~~~~~~~~~~
367375
@@ -394,7 +402,8 @@ a ``number.html.twig`` file inside of it:
394402
395403Welcome to Twig! This simple file already shows off the basics: like how
396404the ``{{ 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.
398407
399408The ``{% extends 'base.html.twig' %} `` points to a layout file that lives
400409at `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
412421``base.html.twig ``.
413422
414423This 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
416425the :doc: `Templating </book/templating >` chapter.
417426
418427Exploring the Project
419428---------------------
420429
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
422431and created a JSON endpoint. Nice!
423432
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:
426435
427436``app/ ``
428437 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
445454`find open source bundles `_ and install them into your project. But even
446455*your * code lives in a bundle - typically ``AppBundle `` (though there's
447456nothing 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.
450459
451460So what about the other directories in the project?
452461
453462``vendor/ ``
454463 Vendor (i.e. third-party) libraries and bundles are downloaded here by
455- `Composer `_ package manager.
464+ the `Composer `_ package manager.
456465
457466``web/ ``
458467 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?
461470
462471.. seealso ::
463472
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 `.
466475
467476Application Configuration
468477-------------------------
@@ -558,8 +567,10 @@ environment, imports and parameters. To learn all of it, see the
558567What's Next?
559568------------
560569
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:
563574
564575* :doc: `/book/controller `
565576* :doc: `/book/routing `
@@ -569,8 +580,8 @@ Then, learn about Symfony's :doc:`service container </book/service_container>`
569580the :doc: `form system </book/forms >`, using :doc: `Doctrine </book/doctrine >`
570581if you need to query a database and more. in the :doc: `Symfony Book </book/index >`.
571582
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.
574585
575586Have fun!
576587
0 commit comments