@@ -431,7 +431,7 @@ content:
431
431
432
432
{
433
433
"require" : {
434
- "symfony/symfony " : " 3.1.* "
434
+ "symfony/http-foundation " : " ^4.0 "
435
435
},
436
436
"autoload" : {
437
437
"files" : [" model.php" ," controllers.php" ]
@@ -534,9 +534,8 @@ a simple application. Along the way, you've made a simple routing
534
534
system and a method using ``ob_start() `` and ``ob_get_clean() `` to render
535
535
templates. If, for some reason, you needed to continue building this "framework"
536
536
from scratch, you could at least use Symfony's standalone
537
- :doc: `Routing </components/routing >` and
538
- :doc: `Templating </components/templating >` components, which already
539
- solve these problems.
537
+ :doc: `Routing </components/routing >` and component and :doc: `Twig </templating >`,
538
+ which already solve these problems.
540
539
541
540
Instead of re-solving common problems, you can let Symfony take care of
542
541
them for you. Here's the same sample application, now built in Symfony::
@@ -549,17 +548,16 @@ them for you. Here's the same sample application, now built in Symfony::
549
548
550
549
class BlogController extends Controller
551
550
{
552
- public function listAction ()
551
+ public function list ()
553
552
{
554
553
$posts = $this->getDoctrine()
555
- ->getManager()
556
- ->createQuery('SELECT p FROM App:Post p')
557
- ->execute();
554
+ ->getRepository(Post::class)
555
+ ->findAll()
558
556
559
- return $this->render('Blog /list.html.php ', array( 'posts' => $posts) );
557
+ return $this->render('blog /list.html.twig ', [ 'posts' => $posts] );
560
558
}
561
559
562
- public function showAction ($id)
560
+ public function show ($id)
563
561
{
564
562
$post = $this->getDoctrine()
565
563
->getRepository(Post::class)
@@ -570,7 +568,7 @@ them for you. Here's the same sample application, now built in Symfony::
570
568
throw $this->createNotFoundException();
571
569
}
572
570
573
- return $this->render('Blog /show.html.php', array( 'post' => $post) );
571
+ return $this->render('blog /show.html.php', [ 'post' => $post] );
574
572
}
575
573
}
576
574
@@ -581,51 +579,49 @@ nice way to group related pages. The controller functions are also sometimes cal
581
579
The two controllers (or actions) are still lightweight. Each uses the
582
580
:doc: `Doctrine ORM library </doctrine >` to retrieve objects from the
583
581
database and the Templating component to render a template and return a
584
- ``Response `` object. The list ``list.php `` template is now quite a bit simpler:
582
+ ``Response `` object. The list ``list.html.twig `` template is now quite a bit simpler,
583
+ and uses Twig:
585
584
586
- .. code-block :: html+php
585
+ .. code-block :: html+twig
587
586
588
- <!-- templates/Blog /list.html.php -->
589
- <?php $view->extend('layout .html.php') ?>
587
+ <!-- templates/blog /list.html.twig -->
588
+ {% extends 'base .html.twig' %}
590
589
591
- <?php $view['slots']->set(' title', ' List of Posts') ?>
590
+ {% block title %} List of Posts{% endblock %}
592
591
593
592
<h1>List of Posts</h1>
594
593
<ul>
595
- <?php foreach ($posts as $post): ?>
594
+ {% for post in posts %}
596
595
<li>
597
- <a href="<?php echo $view['router']->path(
598
- 'blog_show',
599
- array('id' => $post->getId())
600
- ) ?>">
601
- <?= $post->getTitle() ?>
596
+ <a href="{{ path('blog_show', { id: post.id }) }}">
597
+ {{ post.title }}
602
598
</a>
603
599
</li>
604
- <?php endforeach ?>
600
+ {% endfor %}
605
601
</ul>
606
602
607
603
The ``layout.php `` file is nearly identical:
608
604
609
- .. code-block :: html+php
605
+ .. code-block :: html+twig
610
606
611
- <!-- templates/layout .html.php -->
607
+ <!-- templates/base .html.twig -->
612
608
<!DOCTYPE html>
613
609
<html>
614
610
<head>
615
- <title><?= $view['slots']->output(
616
- 'title',
617
- 'Default title'
618
- ) ?></title>
611
+ <meta charset="UTF-8">
612
+ <title>{% block title %}Welcome!{% endblock %}</title>
613
+ {% block stylesheets %}{% endblock %}
619
614
</head>
620
615
<body>
621
- <?= $view['slots']->output('_content') ?>
616
+ {% block body %}{% endblock %}
617
+ {% block javascripts %}{% endblock %}
622
618
</body>
623
619
</html>
624
620
625
621
.. note ::
626
622
627
- The show ``show.php `` template is left as an exercise: updating it should be
628
- really similar to updating the ``list.php `` template.
623
+ The ``show.html.twig `` template is left as an exercise: updating it should be
624
+ really similar to updating the ``list.html.twig `` template.
629
625
630
626
When Symfony's engine (called the Kernel) boots up, it needs a map so
631
627
that it knows which controllers to execute based on the request information.
@@ -637,11 +633,11 @@ in a readable format:
637
633
# config/routes.yaml
638
634
blog_list :
639
635
path : /blog
640
- defaults : { _controller: AppBundle:Blog: list }
636
+ controller : App\Controller\BlogController:: list
641
637
642
638
blog_show :
643
639
path : /blog/show/{id}
644
- defaults : { _controller: AppBundle:Blog: show }
640
+ controller : App\Controller\BlogController:: show
645
641
646
642
Now that Symfony is handling all the mundane tasks, the front controller
647
643
``public/index.php `` is dead simple. And since it does so little, you'll never
@@ -671,54 +667,6 @@ It's a beautiful thing.
671
667
:align: center
672
668
:alt: Symfony request flow
673
669
674
- Better Templates
675
- ~~~~~~~~~~~~~~~~
676
-
677
- If you choose to use it, Symfony comes standard with a templating engine
678
- called `Twig `_ that makes templates faster to write and easier to read.
679
- It means that the sample application could contain even less code! Take,
680
- for example, rewriting ``list.html.php `` template in Twig would look like
681
- this:
682
-
683
- .. code-block :: html+twig
684
-
685
- {# templates/blog/list.html.twig #}
686
- {% extends "layout.html.twig" %}
687
-
688
- {% block title %}List of Posts{% endblock %}
689
-
690
- {% block body %}
691
- <h1>List of Posts</h1>
692
- <ul>
693
- {% for post in posts %}
694
- <li>
695
- <a href="{{ path('blog_show', {'id': post.id}) }}">
696
- {{ post.title }}
697
- </a>
698
- </li>
699
- {% endfor %}
700
- </ul>
701
- {% endblock %}
702
-
703
- And rewriting ``layout.html.php `` template in Twig would look like this:
704
-
705
- .. code-block :: html+twig
706
-
707
- {# templates/layout.html.twig #}
708
- <!DOCTYPE html>
709
- <html>
710
- <head>
711
- <title>{% block title %}Default title{% endblock %}</title>
712
- </head>
713
- <body>
714
- {% block body %}{% endblock %}
715
- </body>
716
- </html>
717
-
718
- Twig is well-supported in Symfony. And while PHP templates will always
719
- be supported in Symfony, the many advantages of Twig will continue to
720
- be discussed. For more information, see the :doc: `templating article </templating >`.
721
-
722
670
Where Symfony Delivers
723
671
----------------------
724
672
0 commit comments