@@ -9,7 +9,7 @@ Symfony versus Flat PHP
99**Why is Symfony better than just opening up a file and writing flat PHP? **
1010
1111If you've never used a PHP framework, aren't familiar with the
12- model-view-controller ` MVC `_ philosophy, or just wonder what all the *hype *
12+ ` Model-View-Controller `_ (MVC) philosophy, or just wonder what all the *hype *
1313is around Symfony, this chapter is for you. Instead of *telling * you that
1414Symfony allows you to develop faster and better software than with flat PHP,
1515you'll see for yourself.
@@ -27,7 +27,7 @@ A Simple Blog in Flat PHP
2727
2828In this chapter, you'll build the token blog application using only flat PHP.
2929To begin, create a single page that displays blog entries that have been
30- persisted to the database. Writing in flat PHP is quick and dirty:
30+ persisted to the database. Writing in flat PHP is quick and dirty::
3131
3232.. code-block :: html+php
3333
@@ -102,8 +102,8 @@ the code that prepares the HTML "presentation"::
102102 require 'templates/list.php';
103103
104104
105- The HTML code is now stored in a separate file ( ``templates/list.php ``) , which
106- is primarily an HTML file that uses a template-like PHP syntax:
105+ The HTML code is now stored in a separate file ``templates/list.php ``, which
106+ is primarily an HTML file that uses a template-like PHP syntax::
107107
108108.. code-block :: html+php
109109
@@ -203,7 +203,7 @@ offering various advantages and the opportunity to reuse almost everything
203203on different pages.
204204
205205The only part of the code that *can't * be reused is the page layout. Fix
206- that by creating a new ``templates/layout.php `` file:
206+ that by creating a new ``templates/layout.php `` file::
207207
208208.. code-block :: html+php
209209
@@ -218,8 +218,8 @@ that by creating a new ``templates/layout.php`` file:
218218 </body>
219219 </html>
220220
221- The template ( ``templates/list.php ``) can now be simplified to "extend"
222- the layout:
221+ The template ``templates/list.php `` can now be simplified to "extend"
222+ the `` templates/ layout.php ``: :
223223
224224.. code-block :: html+php
225225
@@ -256,7 +256,7 @@ and reusable. To prove it, add a blog "show" page, which displays an individual
256256blog post identified by an ``id `` query parameter.
257257
258258To begin, create a new function in the ``model.php `` file that retrieves
259- an individual blog result based on a given `` id `` ::
259+ an individual blog result based on a given id ::
260260
261261 // model.php
262262 function get_post_by_id($id)
@@ -286,7 +286,7 @@ page::
286286 require 'templates/show.php';
287287
288288Finally, create the new template file - ``templates/show.php `` - to render
289- the individual blog post:
289+ the individual blog post::
290290
291291.. code-block :: html+php
292292
@@ -378,7 +378,7 @@ For organization, both controllers (formerly ``index.php`` and ``show.php``)
378378are now PHP functions and each has been moved into a separate file named
379379``controllers.php ``. The job of each PHP function, now called a
380380:term: `controller `, is to use information from the ``Request `` object to create
381- and return a ``Response `` object. ::
381+ and return a ``Response `` object::
382382
383383 // controllers.php
384384 function list_action()
@@ -417,13 +417,15 @@ act a lot like Symfony's mechanism for handling and routing requests.
417417By now, the application has evolved from a single PHP file into a structure
418418that is organized and allows for code reuse. You should be happier, but far
419419from satisfied. For example, the routing system is fickle, and wouldn't
420- recognize that the list page ( ``/index.php ``) should be accessible also via ``/ ``
420+ recognize that the list page - ``/index.php `` - should be accessible also via ``/ ``
421421(if Apache rewrite rules were added). Also, instead of developing the blog,
422422a lot of time is being spent working on the "architecture" of the code (e.g.
423423routing, calling controllers, templates, etc.). More time will need to be
424424spent to handle form submissions, input validation, logging and security.
425425Why should you have to reinvent solutions to all these routine problems?
426426
427+ .. _add-a-touch-of-symfony2 :
428+
427429Add a Touch of Symfony
428430~~~~~~~~~~~~~~~~~~~~~~
429431
@@ -434,7 +436,7 @@ autoloader is a tool that makes it possible to start using PHP classes
434436without explicitly including the file containing the class.
435437
436438In your root directory, create a ``composer.json `` file with the following
437- content:
439+ content::
438440
439441.. code-block :: json
440442
@@ -522,7 +524,8 @@ incidentally, acts quite a bit like the Symfony templating engine::
522524
523525By bringing in a small part of Symfony, the application is more flexible and
524526reliable. The ``Request `` provides a dependable way to access information
525- about the HTTP request. Specifically, the ``getPathInfo() `` method returns
527+ about the HTTP request. Specifically, the
528+ :method: `Symfony\\ Component\\ HttpFoundation\\ Request::getPathInfo ` method returns
526529a cleaned URI (always returning ``/show `` and never ``/index.php/show ``).
527530So, even if the user goes to ``/index.php/show ``, the application is intelligent
528531enough to route the request through ``show_action() ``.
@@ -588,7 +591,7 @@ also called *actions*. They hold code which creates and returns the appropriate
588591The two controllers are still lightweight. Each uses the
589592:doc: `Doctrine ORM library </book/doctrine >` to retrieve objects from the
590593database and the Templating component to render a template and return a
591- ``Response `` object. The list ( ``list.php ``) template is now quite a bit simpler:
594+ ``Response `` object. The list ``list.php `` template is now quite a bit simpler: :
592595
593596.. code-block :: html+php
594597
@@ -611,7 +614,7 @@ database and the Templating component to render a template and return a
611614 <?php endforeach ?>
612615 </ul>
613616
614- The layout ( ``layout.php ``) is nearly identical:
617+ The layout ``layout.php `` is nearly identical: :
615618
616619.. code-block :: html+php
617620
@@ -631,13 +634,13 @@ The layout (``layout.php``) is nearly identical:
631634
632635.. note ::
633636
634- The show ( ``show.php ``) template is left as an exercise, as it should be trivial to
635- create based on the list ( ``list.php ``) template.
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.
636639
637640When Symfony's engine (called the :term: `Kernel `) boots up, it needs a map so
638641that it knows which controllers to execute based on the request information.
639642A routing configuration map ``app/config/routing.yml `` provides this information
640- in a readable format:
643+ in a readable format::
641644
642645.. code-block :: yaml
643646
@@ -677,47 +680,14 @@ object are sent back to the client.
677680 :align: center
678681 :alt: Symfony request flow
679682
680- Where Symfony Delivers
681- ~~~~~~~~~~~~~~~~~~~~~~
682-
683- In the upcoming chapters, you'll learn more about how each piece of Symfony
684- works and the recommended organization of a project. For now, have a look
685- at how migrating the blog from flat PHP to Symfony has improved life:
686-
687- * Your application now has **clear and consistently organized code ** (though
688- Symfony doesn't force you into this). This promotes **reusability ** and
689- allows for new developers to be productive in your project more quickly;
690-
691- * 100% of the code you write is for *your * application. You **don't need
692- to develop or maintain low-level utilities ** such as autoloading,
693- :doc: `routing </book/routing >`, or rendering :doc: `controllers </book/controller >`;
694-
695- * Symfony gives you **access to open source tools ** such as `Doctrine `_ and the
696- :doc: `Templating </components/templating/introduction >`,
697- :doc: `Security </components/security/introduction >`,
698- :doc: `Form </components/form/introduction >`, `Validator `_ and
699- :doc: `Translation </components/translation/introduction >` components (to name
700- a few);
701-
702- * The application now enjoys **fully-flexible URLs ** thanks to the Routing
703- component;
704-
705- * Symfony's HTTP-centric architecture gives you access to powerful tools
706- such as **HTTP caching ** powered by **Symfony's internal HTTP cache ** or
707- more powerful tools such as `Varnish `_. This is covered in a later chapter
708- all about :doc: `caching </book/http_cache >`.
709-
710- And perhaps best of all, by using Symfony, you now have access to a whole
711- set of **high-quality open source tools developed by the Symfony community **!
712- A good selection of Symfony community tools can be found on `KnpBundles.com `_.
713-
714- Better Templates
715- ----------------
683+ PHP Templates versus Twig Templates
684+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
716685
717686If you choose to use it, Symfony comes standard with a templating engine
718687called `Twig `_ that makes templates faster to write and easier to read.
719688It means that the sample application could contain even less code! Take,
720- for example, lets rewrite ``layout.html.php `` template in Twig:
689+ for example, rewriting ``list.html.php `` template in Twig would look like
690+ this::
721691
722692.. code-block :: html+twig
723693
@@ -739,7 +709,7 @@ for example, lets rewrite ``layout.html.php`` template in Twig:
739709 </ul>
740710 {% endblock %}
741711
742- The corresponding ``layout.html.php `` template is also easier to write in Twig :
712+ And rewriting ``layout.html.php `` template in Twig would look like this: :
743713
744714.. code-block :: html+twig
745715
@@ -758,14 +728,49 @@ Twig is well-supported in Symfony. And while PHP templates will always
758728be supported in Symfony, the many advantages of Twig will continue to
759729be discussed. For more information, see the :doc: `templating chapter </book/templating >`.
760730
731+
732+ Where Symfony Delivers
733+ ----------------------
734+
735+ 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:
738+
739+ * Your application now has **clear and consistently organized code ** (though
740+ Symfony doesn't force you into this). This promotes **reusability ** and
741+ allows for new developers to be productive in your project more quickly;
742+
743+ * 100% of the code you write is for *your * application. You **don't need
744+ to develop or maintain low-level utilities ** such as autoloading,
745+ :doc: `routing </book/routing >`, or rendering :doc: `controllers </book/controller >`;
746+
747+ * Symfony gives you **access to open source tools ** such as `Doctrine `_ and the
748+ :doc: `Templating </components/templating/introduction >`,
749+ :doc: `Security </components/security/introduction >`,
750+ :doc: `Form </components/form/introduction >`, `Validator `_ and
751+ :doc: `Translation </components/translation/introduction >` components (to name
752+ a few);
753+
754+ * The application now enjoys **fully-flexible URLs ** thanks to the Routing
755+ component;
756+
757+ * Symfony's HTTP-centric architecture gives you access to powerful tools
758+ such as **HTTP caching ** powered by **Symfony's internal HTTP cache ** or
759+ more powerful tools such as `Varnish `_. This is covered in a later chapter
760+ all about :doc: `caching </book/http_cache >`.
761+
762+ And perhaps best of all, by using Symfony, you now have access to a whole
763+ set of **high-quality open source tools developed by the Symfony community **!
764+ A good selection of Symfony community tools can be found on `KnpBundles.com `_.
765+
761766Learn more from the Cookbook
762767----------------------------
763768
764769* :doc: `/cookbook/templating/PHP `
765770* :doc: `/cookbook/controller/service `
766771
767772
768- .. _`MVC ` : https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
773+ .. _`Model-View-Controller ` : https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
769774.. _`Doctrine` : http://www.doctrine-project.org
770775.. _`SQL injection attack` : https://en.wikipedia.org/wiki/SQL_injection
771776.. _`Composer` : https://getcomposer.org
0 commit comments