@@ -431,7 +431,7 @@ content:
431431
432432 {
433433 "require" : {
434- "symfony/symfony " : " 3.1.* "
434+ "symfony/http-foundation " : " ^4.0 "
435435 },
436436 "autoload" : {
437437 "files" : [" model.php" ," controllers.php" ]
@@ -534,9 +534,8 @@ a simple application. Along the way, you've made a simple routing
534534system and a method using ``ob_start() `` and ``ob_get_clean() `` to render
535535templates. If, for some reason, you needed to continue building this "framework"
536536from 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.
540539
541540Instead of re-solving common problems, you can let Symfony take care of
542541them 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::
549548
550549 class BlogController extends Controller
551550 {
552- public function listAction ()
551+ public function list ()
553552 {
554553 $posts = $this->getDoctrine()
555- ->getManager()
556- ->createQuery('SELECT p FROM App:Post p')
557- ->execute();
554+ ->getRepository(Post::class)
555+ ->findAll()
558556
559- return $this->render('Blog /list.html.php ', array( 'posts' => $posts) );
557+ return $this->render('blog /list.html.twig ', [ 'posts' => $posts] );
560558 }
561559
562- public function showAction ($id)
560+ public function show ($id)
563561 {
564562 $post = $this->getDoctrine()
565563 ->getRepository(Post::class)
@@ -570,7 +568,7 @@ them for you. Here's the same sample application, now built in Symfony::
570568 throw $this->createNotFoundException();
571569 }
572570
573- return $this->render('Blog /show.html.php', array( 'post' => $post) );
571+ return $this->render('blog /show.html.php', [ 'post' => $post] );
574572 }
575573 }
576574
@@ -581,51 +579,49 @@ nice way to group related pages. The controller functions are also sometimes cal
581579The two controllers (or actions) are still lightweight. Each uses the
582580:doc: `Doctrine ORM library </doctrine >` to retrieve objects from the
583581database 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:
585584
586- .. code-block :: html+php
585+ .. code-block :: html+twig
587586
588- <!-- templates/Blog /list.html.php -->
589- <?php $view->extend('layout .html.php') ?>
587+ <!-- templates/blog /list.html.twig -->
588+ {% extends 'base .html.twig' %}
590589
591- <?php $view['slots']->set(' title', ' List of Posts') ?>
590+ {% block title %} List of Posts{% endblock %}
592591
593592 <h1>List of Posts</h1>
594593 <ul>
595- <?php foreach ($posts as $post): ?>
594+ {% for post in posts %}
596595 <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 }}
602598 </a>
603599 </li>
604- <?php endforeach ?>
600+ {% endfor %}
605601 </ul>
606602
607603The ``layout.php `` file is nearly identical:
608604
609- .. code-block :: html+php
605+ .. code-block :: html+twig
610606
611- <!-- templates/layout .html.php -->
607+ <!-- templates/base .html.twig -->
612608 <!DOCTYPE html>
613609 <html>
614610 <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 %}
619614 </head>
620615 <body>
621- <?= $view['slots']->output('_content') ?>
616+ {% block body %}{% endblock %}
617+ {% block javascripts %}{% endblock %}
622618 </body>
623619 </html>
624620
625621.. note ::
626622
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.
629625
630626When Symfony's engine (called the Kernel) boots up, it needs a map so
631627that it knows which controllers to execute based on the request information.
@@ -637,11 +633,11 @@ in a readable format:
637633 # config/routes.yaml
638634 blog_list :
639635 path : /blog
640- defaults : { _controller: AppBundle:Blog: list }
636+ controller : App\Controller\BlogController:: list
641637
642638 blog_show :
643639 path : /blog/show/{id}
644- defaults : { _controller: AppBundle:Blog: show }
640+ controller : App\Controller\BlogController:: show
645641
646642 Now that Symfony is handling all the mundane tasks, the front controller
647643``public/index.php `` is dead simple. And since it does so little, you'll never
@@ -671,54 +667,6 @@ It's a beautiful thing.
671667 :align: center
672668 :alt: Symfony request flow
673669
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-
722670Where Symfony Delivers
723671----------------------
724672
0 commit comments