@@ -102,7 +102,7 @@ the code that prepares the HTML "presentation"::
102
102
require 'templates/list.php';
103
103
104
104
105
- The HTML code is now stored in a separate ``templates/list.php `` file , which
105
+ The HTML code is now stored in a separate file ``templates/list.php ``, which
106
106
is primarily an HTML file that uses a template-like PHP syntax:
107
107
108
108
.. code-block :: html+php
@@ -338,9 +338,8 @@ application change slightly, but start to become more flexible:
338
338
339
339
.. tip ::
340
340
341
- Using Apache's ``mod_rewrite `` (or equivalent with other web servers),
342
- the URL can easily be cleaned up - ``index.php `` portion removed -
343
- to be just ``/show ``.
341
+ By using rewrite rules in your :doc: `web server configuration </cookbook/configuration/web_server_configuration >`,
342
+ the ``index.php `` won't be needed and you will have beautiful, clean URLs (e.g. ``/show ``).
344
343
345
344
When using a front controller, a single PHP file (``index.php `` in this case)
346
345
renders *every * request. For the blog post show page, ``/index.php/show `` will
@@ -375,10 +374,7 @@ on the requested URI::
375
374
}
376
375
377
376
For organization, both controllers (formerly ``index.php `` and ``show.php ``)
378
- are now PHP functions and each has been moved into a separate file named
379
- ``controllers.php ``. The job of each PHP function, now called a
380
- :term: `controller `, is to use information from the ``Request `` object to create
381
- and return a ``Response `` object::
377
+ are now PHP functions and each has been moved into a separate file named ``controllers.php ``::
382
378
383
379
// controllers.php
384
380
function list_action()
@@ -397,15 +393,11 @@ As a front controller, ``index.php`` has taken on an entirely new role, one
397
393
that includes loading the core libraries and routing the application so that
398
394
one of the two controllers (the ``list_action() `` and ``show_action() ``
399
395
functions) is called. In reality, the front controller is beginning to look and
400
- act a lot like Symfony's mechanism for handling and routing requests.
396
+ act a lot like how Symfony handles and routes requests.
401
397
402
- .. note ::
403
-
404
- Though similarly named, a "front controller" is different from the PHP functions
405
- called "controllers" talked about in this chapter. A front controller is a short PHP
406
- file through which all requests are directed. "Controller" functions are grouped in
407
- several files and they hold your code which creates and returns the appropriate
408
- ``Response `` object. Controllers are also called *actions *.
398
+ But but careful not to confuse the terms *front controller * and *controller *. Your
399
+ app will usually have just *one * front controller, which boots your code. You will
400
+ have *many * controller functions: one for each page.
409
401
410
402
.. tip ::
411
403
@@ -458,7 +450,7 @@ into a ``vendor/`` directory:
458
450
459
451
Beside downloading your dependencies, Composer generates a ``vendor/autoload.php `` file,
460
452
which takes care of autoloading for all the files in the Symfony Framework as well as
461
- the files mentioned in the `` autoload `` section of your ``composer.json ``.
453
+ the files mentioned in the autoload section of your ``composer.json ``.
462
454
463
455
Core to Symfony's philosophy is the idea that an application's main job is
464
456
to interpret each request and return a response. To this end, Symfony provides
@@ -535,6 +527,8 @@ allowing HTTP headers and content to be added via an object-oriented interface.
535
527
And while the responses in this application are simple, this flexibility
536
528
will pay dividends as your application grows.
537
529
530
+ .. _the-sample-application-in-symfony2 :
531
+
538
532
The Sample Application in Symfony
539
533
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
540
534
@@ -583,12 +577,11 @@ them for you. Here's the same sample application, now built in Symfony::
583
577
}
584
578
}
585
579
586
- First we have a "controller class" which is a convenient way to group several
587
- "controllers" together. So methods inside a controller class are controllers
588
- also called *actions *. They hold code which creates and returns the appropriate
589
- ``Response `` object.
580
+ Notice, both controller functions now live inside a "controller class". This is a
581
+ nice way to group related pages. The controller functions are also sometimes called
582
+ *actions *.
590
583
591
- The two controllers are still lightweight. Each uses the
584
+ The two controllers (or actions) are still lightweight. Each uses the
592
585
:doc: `Doctrine ORM library </book/doctrine >` to retrieve objects from the
593
586
database and the Templating component to render a template and return a
594
587
``Response `` object. The list ``list.php `` template is now quite a bit simpler:
@@ -634,12 +627,12 @@ The layout ``layout.php`` is nearly identical:
634
627
635
628
.. note ::
636
629
637
- The show ``show.php `` template is left as an exercise, as it should be trivial to
638
- create based on the list ``list.php `` template.
630
+ The show ``show.php `` template is left as an exercise: updating it should be
631
+ really similar to updating the ``list.php `` template.
639
632
640
633
When Symfony's engine (called the :term: `Kernel `) boots up, it needs a map so
641
634
that it knows which controllers to execute based on the request information.
642
- A routing configuration map ``app/config/routing.yml `` provides this information
635
+ A routing configuration map - ``app/config/routing.yml `` - provides this information
643
636
in a readable format:
644
637
645
638
.. code-block :: yaml
@@ -655,8 +648,7 @@ in a readable format:
655
648
656
649
Now that Symfony is handling all the mundane tasks, the front controller
657
650
``web/app.php `` is dead simple. And since it does so little, you'll never
658
- have to touch it once it's created (and if you use a `Symfony distribution `_,
659
- you won't even need to create it!)::
651
+ have to touch it::
660
652
661
653
// web/app.php
662
654
require_once __DIR__.'/../app/bootstrap.php';
@@ -667,21 +659,23 @@ you won't even need to create it!)::
667
659
$kernel = new AppKernel('prod', false);
668
660
$kernel->handle(Request::createFromGlobals())->send();
669
661
670
- Front controller's only job is to initialize Symfony's engine (called the
662
+ The front controller's only job is to initialize Symfony's engine (called the
671
663
:term: `Kernel `) and pass it a ``Request `` object to handle. The Symfony core
672
664
asks the router to inspect the request. The router matches the incoming URL
673
665
to a specific route and returns information about the route, including the
674
666
controller that should be executed. The correct controller from the matched
675
- route is executed and the code inside the controller creates and returns the
667
+ route is executed and your code inside the controller creates and returns the
676
668
appropriate ``Response `` object. The HTTP headers and content of the ``Response ``
677
669
object are sent back to the client.
678
670
671
+ It's a beautiful thing.
672
+
679
673
.. figure :: /images/request-flow.png
680
674
:align: center
681
675
:alt: Symfony request flow
682
676
683
- PHP Templates versus Twig Templates
684
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
677
+ Better Templates
678
+ ~~~~~~~~~~~~~~~~
685
679
686
680
If you choose to use it, Symfony comes standard with a templating engine
687
681
called `Twig `_ that makes templates faster to write and easier to read.
@@ -726,15 +720,14 @@ And rewriting ``layout.html.php`` template in Twig would look like this:
726
720
727
721
Twig is well-supported in Symfony. And while PHP templates will always
728
722
be supported in Symfony, the many advantages of Twig will continue to
729
- be discussed. For more information, see the :doc: `Templating chapter </book/templating >`.
730
-
723
+ be discussed. For more information, see the :doc: `templating chapter </book/templating >`.
731
724
732
725
Where Symfony Delivers
733
726
----------------------
734
727
735
728
In the upcoming chapters, you'll learn more about how each piece of Symfony
736
- works and the recommended organization of a project. For now, have a look
737
- at how migrating the blog from flat PHP to Symfony has improved life:
729
+ works and how you can organize your project. For now, celebrate at how migrating
730
+ the blog from flat PHP to Symfony has improved life:
738
731
739
732
* Your application now has **clear and consistently organized code ** (though
740
733
Symfony doesn't force you into this). This promotes **reusability ** and
@@ -769,14 +762,12 @@ Learn more from the Cookbook
769
762
* :doc: `/cookbook/templating/PHP `
770
763
* :doc: `/cookbook/controller/service `
771
764
772
-
773
765
.. _`Model-View-Controller` : https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
774
766
.. _`Doctrine` : http://www.doctrine-project.org
775
767
.. _`SQL injection attack` : https://en.wikipedia.org/wiki/SQL_injection
776
768
.. _`Composer` : https://getcomposer.org
777
769
.. _`download Composer` : https://getcomposer.org/download/
778
- .. _`Symfony distribution` : https://github.com/symfony/symfony-standard
779
- .. _`Twig` : http://twig.sensiolabs.org
780
770
.. _`Validator` : https://github.com/symfony/validator
781
771
.. _`Varnish` : https://www.varnish-cache.org/
782
- .. _`KnpBundles.com` : http://knpbundles.com/
772
+ .. _`KnpBundles.com` : http://knpbundles.com/
773
+ .. _`Twig` : http://twig.sensiolabs.org
0 commit comments