@@ -9,7 +9,7 @@ Symfony versus Flat PHP
9
9
**Why is Symfony better than just opening up a file and writing flat PHP? **
10
10
11
11
If 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 *
13
13
is around Symfony, this chapter is for you. Instead of *telling * you that
14
14
Symfony allows you to develop faster and better software than with flat PHP,
15
15
you'll see for yourself.
@@ -27,7 +27,7 @@ A Simple Blog in Flat PHP
27
27
28
28
In this chapter, you'll build the token blog application using only flat PHP.
29
29
To 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::
31
31
32
32
.. code-block :: html+php
33
33
@@ -102,8 +102,8 @@ 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 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::
107
107
108
108
.. code-block :: html+php
109
109
@@ -203,7 +203,7 @@ offering various advantages and the opportunity to reuse almost everything
203
203
on different pages.
204
204
205
205
The 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::
207
207
208
208
.. code-block :: html+php
209
209
@@ -218,8 +218,8 @@ that by creating a new ``templates/layout.php`` file:
218
218
</body>
219
219
</html>
220
220
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 ``: :
223
223
224
224
.. code-block :: html+php
225
225
@@ -256,7 +256,7 @@ and reusable. To prove it, add a blog "show" page, which displays an individual
256
256
blog post identified by an ``id `` query parameter.
257
257
258
258
To 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 ::
260
260
261
261
// model.php
262
262
function get_post_by_id($id)
@@ -286,7 +286,7 @@ page::
286
286
require 'templates/show.php';
287
287
288
288
Finally, create the new template file - ``templates/show.php `` - to render
289
- the individual blog post:
289
+ the individual blog post::
290
290
291
291
.. code-block :: html+php
292
292
@@ -378,7 +378,7 @@ For organization, both controllers (formerly ``index.php`` and ``show.php``)
378
378
are now PHP functions and each has been moved into a separate file named
379
379
``controllers.php ``. The job of each PHP function, now called a
380
380
:term: `controller `, is to use information from the ``Request `` object to create
381
- and return a ``Response `` object. ::
381
+ and return a ``Response `` object::
382
382
383
383
// controllers.php
384
384
function list_action()
@@ -417,13 +417,15 @@ act a lot like Symfony's mechanism for handling and routing requests.
417
417
By now, the application has evolved from a single PHP file into a structure
418
418
that is organized and allows for code reuse. You should be happier, but far
419
419
from 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 ``/ ``
421
421
(if Apache rewrite rules were added). Also, instead of developing the blog,
422
422
a lot of time is being spent working on the "architecture" of the code (e.g.
423
423
routing, calling controllers, templates, etc.). More time will need to be
424
424
spent to handle form submissions, input validation, logging and security.
425
425
Why should you have to reinvent solutions to all these routine problems?
426
426
427
+ .. _add-a-touch-of-symfony2 :
428
+
427
429
Add a Touch of Symfony
428
430
~~~~~~~~~~~~~~~~~~~~~~
429
431
@@ -434,7 +436,7 @@ autoloader is a tool that makes it possible to start using PHP classes
434
436
without explicitly including the file containing the class.
435
437
436
438
In your root directory, create a ``composer.json `` file with the following
437
- content:
439
+ content::
438
440
439
441
.. code-block :: json
440
442
@@ -522,7 +524,8 @@ incidentally, acts quite a bit like the Symfony templating engine::
522
524
523
525
By bringing in a small part of Symfony, the application is more flexible and
524
526
reliable. 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
526
529
a cleaned URI (always returning ``/show `` and never ``/index.php/show ``).
527
530
So, even if the user goes to ``/index.php/show ``, the application is intelligent
528
531
enough to route the request through ``show_action() ``.
@@ -588,7 +591,7 @@ also called *actions*. They hold code which creates and returns the appropriate
588
591
The two controllers are still lightweight. Each uses the
589
592
:doc: `Doctrine ORM library </book/doctrine >` to retrieve objects from the
590
593
database 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: :
592
595
593
596
.. code-block :: html+php
594
597
@@ -611,7 +614,7 @@ database and the Templating component to render a template and return a
611
614
<?php endforeach ?>
612
615
</ul>
613
616
614
- The layout ( ``layout.php ``) is nearly identical:
617
+ The layout ``layout.php `` is nearly identical: :
615
618
616
619
.. code-block :: html+php
617
620
@@ -631,13 +634,13 @@ The layout (``layout.php``) is nearly identical:
631
634
632
635
.. note ::
633
636
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.
636
639
637
640
When Symfony's engine (called the :term: `Kernel `) boots up, it needs a map so
638
641
that it knows which controllers to execute based on the request information.
639
642
A routing configuration map ``app/config/routing.yml `` provides this information
640
- in a readable format:
643
+ in a readable format::
641
644
642
645
.. code-block :: yaml
643
646
@@ -677,47 +680,14 @@ object are sent back to the client.
677
680
:align: center
678
681
:alt: Symfony request flow
679
682
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
716
685
717
686
If you choose to use it, Symfony comes standard with a templating engine
718
687
called `Twig `_ that makes templates faster to write and easier to read.
719
688
It 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::
721
691
722
692
.. code-block :: html+twig
723
693
@@ -739,7 +709,7 @@ for example, lets rewrite ``layout.html.php`` template in Twig:
739
709
</ul>
740
710
{% endblock %}
741
711
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: :
743
713
744
714
.. code-block :: html+twig
745
715
@@ -758,14 +728,49 @@ Twig is well-supported in Symfony. And while PHP templates will always
758
728
be supported in Symfony, the many advantages of Twig will continue to
759
729
be discussed. For more information, see the :doc: `templating chapter </book/templating >`.
760
730
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
+
761
766
Learn more from the Cookbook
762
767
----------------------------
763
768
764
769
* :doc: `/cookbook/templating/PHP `
765
770
* :doc: `/cookbook/controller/service `
766
771
767
772
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
769
774
.. _`Doctrine` : http://www.doctrine-project.org
770
775
.. _`SQL injection attack` : https://en.wikipedia.org/wiki/SQL_injection
771
776
.. _`Composer` : https://getcomposer.org
0 commit comments