Skip to content

Commit d971f87

Browse files
committed
minor #8734 Updated templating/* articles to Symfony 4 (javiereguiluz)
This PR was squashed before being merged into the 4.0 branch (closes #8734). Discussion ---------- Updated templating/* articles to Symfony 4 Commits ------- 550cd58 Fixed PHP templates location d0ede99 Updated templating/* articles to Symfony 4
2 parents 3e891d0 + 550cd58 commit d971f87

12 files changed

+138
-187
lines changed

templating.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ configuration:
575575
/**
576576
* @Route("/", name="welcome")
577577
*/
578-
public function indexAction()
578+
public function index()
579579
{
580580
// ...
581581
}
@@ -586,19 +586,19 @@ configuration:
586586
# config/routes.yaml
587587
welcome:
588588
path: /
589-
defaults: { _controller: AppBundle:Welcome:index }
589+
controller: App\Controller\WelcomeController::index
590590
591591
.. code-block:: xml
592592
593-
<!-- config/routes.yaml -->
593+
<!-- config/routes.xml -->
594594
<?xml version="1.0" encoding="UTF-8" ?>
595595
<routes xmlns="http://symfony.com/schema/routing"
596596
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
597597
xsi:schemaLocation="http://symfony.com/schema/routing
598598
http://symfony.com/schema/routing/routing-1.0.xsd">
599599
600600
<route id="welcome" path="/">
601-
<default key="_controller">AppBundle:Welcome:index</default>
601+
<default key="_controller">App\Controller\WelcomeController::index</default>
602602
</route>
603603
</routes>
604604
@@ -610,7 +610,7 @@ configuration:
610610
611611
$collection = new RouteCollection();
612612
$collection->add('welcome', new Route('/', array(
613-
'_controller' => 'AppBundle:Welcome:index',
613+
'_controller' => 'App\Controller\WelcomeController::index',
614614
)));
615615
616616
return $collection;
@@ -644,7 +644,7 @@ route:
644644
/**
645645
* @Route("/article/{slug}", name="article_show")
646646
*/
647-
public function showAction($slug)
647+
public function show($slug)
648648
{
649649
// ...
650650
}
@@ -654,8 +654,8 @@ route:
654654
655655
# config/routes.yaml
656656
article_show:
657-
path: /article/{slug}
658-
defaults: { _controller: AppBundle:Article:show }
657+
path: /article/{slug}
658+
controller: App\Controller\ArticleController::show
659659
660660
.. code-block:: xml
661661
@@ -667,7 +667,7 @@ route:
667667
http://symfony.com/schema/routing/routing-1.0.xsd">
668668
669669
<route id="article_show" path="/article/{slug}">
670-
<default key="_controller">AppBundle:Article:show</default>
670+
<default key="_controller">App\Controller\ArticleController::show</default>
671671
</route>
672672
</routes>
673673
@@ -679,7 +679,7 @@ route:
679679
680680
$collection = new RouteCollection();
681681
$collection->add('article_show', new Route('/article/{slug}', array(
682-
'_controller' => 'AppBundle:Article:show',
682+
'_controller' => 'App\Controller\ArticleController::show',
683683
)));
684684
685685
return $collection;
@@ -791,10 +791,10 @@ advantage of Symfony's template inheritance.
791791
.. tip::
792792

793793
This section will teach you the philosophy behind including stylesheet
794-
and JavaScript assets in Symfony. Symfony is also compatible with another
795-
library, called Assetic, which follows this philosophy but allows you to do
796-
much more interesting things with those assets. For more information on
797-
using Assetic see :doc:`/frontend/assetic/asset_management`.
794+
and JavaScript assets in Symfony. If you are interested in compiling and
795+
creating those assets, check out the :doc:`Webpack Encore documentation </frontend>`
796+
a tool that seamlessly integrates Webpack and other modern JavaScript tools
797+
into Symfony applications.
798798

799799
Start by adding two blocks to your base template that will hold your assets:
800800
one called ``stylesheets`` inside the ``head`` tag and another called ``javascripts``
@@ -878,7 +878,7 @@ to add to the parent block's content (and not actually *replace* it), you
878878
should use the ``parent()`` Twig function to include everything from the ``stylesheets``
879879
block of the base template.
880880

881-
You can also include assets located in your bundles' ``Resources/public`` folder.
881+
You can also include assets located in your bundles' ``Resources/public/`` folder.
882882
You will need to run the ``php bin/console assets:install target [--symlink]``
883883
command, which copies (or symlinks) files into the correct location. (target
884884
is by default the "web/" directory of your application).

templating/PHP.rst

Lines changed: 39 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ your application configuration file:
2424

2525
.. code-block:: yaml
2626
27-
# app/config/config.yml
27+
# config/packages/framework.yaml
2828
framework:
2929
# ...
3030
templating:
3131
engines: ['twig', 'php']
3232
3333
.. code-block:: xml
3434
35-
<!-- app/config/config.xml -->
35+
<!-- config/packages/framework.xml -->
3636
<?xml version="1.0" encoding="UTF-8" ?>
3737
<container xmlns="http://symfony.com/schema/dic/services"
3838
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -53,6 +53,7 @@ your application configuration file:
5353
5454
.. code-block:: php
5555
56+
// config/packages/framework.php
5657
$container->loadFromExtension('framework', array(
5758
// ...
5859
'templating' => array(
@@ -67,28 +68,12 @@ below renders the ``index.html.php`` template::
6768
// src/Controller/HelloController.php
6869

6970
// ...
70-
public function indexAction($name)
71+
public function index($name)
7172
{
72-
return $this->render(
73-
'AppBundle:Hello:index.html.php',
74-
array('name' => $name)
75-
);
76-
}
77-
78-
You can also use the `@Template`_ shortcut to render the default
79-
``AppBundle:Hello:index.html.php`` template::
80-
81-
// src/Controller/HelloController.php
82-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
83-
84-
// ...
85-
86-
/**
87-
* @Template(engine="php")
88-
*/
89-
public function indexAction($name)
90-
{
91-
return array('name' => $name);
73+
// template is stored in src/Resources/views/hello/index.html.php
74+
return $this->render('hello/index.html.php', array(
75+
'name' => $name
76+
));
9277
}
9378

9479
.. caution::
@@ -98,24 +83,24 @@ You can also use the `@Template`_ shortcut to render the default
9883
the ``@`` notation for Twig namespaces will no longer be supported for the
9984
``render()`` method::
10085

101-
public function indexAction()
86+
public function index()
10287
{
10388
// ...
10489

10590
// namespaced templates will no longer work in controllers
106-
$this->render('@App/Default/index.html.twig');
91+
$this->render('@SomeNamespace/hello/index.html.twig');
10792

10893
// you must use the traditional template notation
109-
$this->render('AppBundle:Default:index.html.twig');
94+
$this->render('hello/index.html.twig');
11095
}
11196

11297
.. code-block:: twig
11398
11499
{# inside a Twig template, namespaced templates work as expected #}
115-
{{ include('@App/Default/index.html.twig') }}
100+
{{ include('@SomeNamespace/hello/index.html.twig') }}
116101
117102
{# traditional template notation will also work #}
118-
{{ include('AppBundle:Default:index.html.twig') }}
103+
{{ include('hello/index.html.twig') }}
119104
120105
121106
.. index::
@@ -134,36 +119,29 @@ the ``extend()`` call:
134119

135120
.. code-block:: html+php
136121

137-
<!-- templates/Hello/index.html.php -->
138-
<?php $view->extend('AppBundle::layout.html.php') ?>
122+
<!-- src/Resources/views/hello/index.html.php -->
123+
<?php $view->extend('layout.html.php') ?>
139124

140125
Hello <?php echo $name ?>!
141126

142-
The ``AppBundle::layout.html.php`` notation sounds familiar, doesn't it? It
143-
is the same notation used to reference a template. The ``::`` part simply
144-
means that the controller element is empty, so the corresponding file is
145-
directly stored under ``views/``.
146-
147127
Now, have a look at the ``layout.html.php`` file:
148128

149129
.. code-block:: html+php
150130

151-
<!-- templates/layout.html.php -->
152-
<?php $view->extend('::base.html.php') ?>
131+
<!-- src/Resources/views/layout.html.php -->
132+
<?php $view->extend('base.html.php') ?>
153133

154134
<h1>Hello Application</h1>
155135

156136
<?php $view['slots']->output('_content') ?>
157137

158-
The layout is itself decorated by another one (``::base.html.php``). Symfony
138+
The layout is itself decorated by another one (``base.html.php``). Symfony
159139
supports multiple decoration levels: a layout can itself be decorated by
160-
another one. When the bundle part of the template name is empty, views are
161-
looked for in the ``templates/`` directory. This directory stores
162-
global views for your entire project:
140+
another one:
163141

164142
.. code-block:: html+php
165143

166-
<!-- templates/base.html.php -->
144+
<!-- src/Resources/views/base.html.php -->
167145
<!DOCTYPE html>
168146
<html>
169147
<head>
@@ -196,8 +174,8 @@ decorating the template. In the ``index.html.php`` template, define a
196174

197175
.. code-block:: html+php
198176

199-
<!-- templates/Hello/index.html.php -->
200-
<?php $view->extend('AppBundle::layout.html.php') ?>
177+
<!-- src/Resources/views/hello/index.html.php -->
178+
<?php $view->extend('layout.html.php') ?>
201179

202180
<?php $view['slots']->set('title', 'Hello World Application') ?>
203181

@@ -207,7 +185,7 @@ The base layout already has the code to output the title in the header:
207185

208186
.. code-block:: html+php
209187

210-
<!-- templates/base.html.php -->
188+
<!-- src/Resources/views/base.html.php -->
211189
<head>
212190
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
213191
<title><?php $view['slots']->output('title', 'Hello Application') ?></title>
@@ -238,17 +216,17 @@ Create a ``hello.html.php`` template:
238216

239217
.. code-block:: html+php
240218

241-
<!-- templates/Hello/hello.html.php -->
219+
<!-- src/Resources/views/hello/hello.html.php -->
242220
Hello <?php echo $name ?>!
243221

244222
And change the ``index.html.php`` template to include it:
245223

246224
.. code-block:: html+php
247225

248-
<!-- templates/Hello/index.html.php -->
249-
<?php $view->extend('AppBundle::layout.html.php') ?>
226+
<!-- src/Resources/views/hello/index.html.php -->
227+
<?php $view->extend('layout.html.php') ?>
250228

251-
<?php echo $view->render('AppBundle:Hello:hello.html.php', array('name' => $name)) ?>
229+
<?php echo $view->render('hello/hello.html.php', array('name' => $name)) ?>
252230

253231
The ``render()`` method evaluates and returns the content of another template
254232
(this is the exact same method as the one used in the controller).
@@ -268,35 +246,17 @@ If you create a ``fancy`` action, and want to include it into the
268246

269247
.. code-block:: html+php
270248

271-
<!-- templates/Hello/index.html.php -->
249+
<!-- src/Resources/views/hello/index.html.php -->
272250
<?php echo $view['actions']->render(
273-
new \Symfony\Component\HttpKernel\Controller\ControllerReference('AppBundle:Hello:fancy', array(
274-
'name' => $name,
275-
'color' => 'green',
276-
))
251+
new \Symfony\Component\HttpKernel\Controller\ControllerReference(
252+
'App\Controller\HelloController::fancy',
253+
array(
254+
'name' => $name,
255+
'color' => 'green',
256+
)
257+
)
277258
) ?>
278259

279-
Here, the ``AppBundle:Hello:fancy`` string refers to the ``fancy`` action of the
280-
``Hello`` controller::
281-
282-
// src/Controller/HelloController.php
283-
284-
class HelloController extends Controller
285-
{
286-
public function fancyAction($name, $color)
287-
{
288-
// create some object, based on the $color variable
289-
$object = ...;
290-
291-
return $this->render('AppBundle:Hello:fancy.html.php', array(
292-
'name' => $name,
293-
'object' => $object
294-
));
295-
}
296-
297-
// ...
298-
}
299-
300260
But where is the ``$view['actions']`` array element defined? Like
301261
``$view['slots']``, it's called a template helper, and the next section tells
302262
you more about those.
@@ -332,10 +292,10 @@ pattern:
332292

333293
.. code-block:: yaml
334294
335-
# src/Resources/config/routing.yml
336-
hello: # The route name
337-
path: /hello/{name}
338-
defaults: { _controller: AppBundle:Hello:index }
295+
# config/routes.yaml
296+
hello:
297+
path: /hello/{name}
298+
controller: App\Controller\HelloController::index
339299
340300
Using Assets: Images, JavaScripts and Stylesheets
341301
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

templating/debug.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
How to Dump Debug Information in Twig Templates
88
===============================================
99

10-
When using PHP, you can use the
10+
When using PHP templates, you can use the
1111
:ref:`dump() function from the VarDumper component <components-var-dumper-dump>`
1212
if you need to quickly find the value of a variable passed. This is useful,
1313
for example, inside your controller::
@@ -19,7 +19,7 @@ for example, inside your controller::
1919

2020
class ArticleController extends Controller
2121
{
22-
public function recentListAction()
22+
public function recentList()
2323
{
2424
$articles = ...;
2525
dump($articles);

templating/embedding_controllers.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ articles::
2626

2727
class ArticleController extends Controller
2828
{
29-
public function recentArticlesAction($max = 3)
29+
public function recentArticles($max = 3)
3030
{
3131
// make a database call or other logic
3232
// to get the "$max" most recent articles
@@ -68,7 +68,7 @@ The ``recent_list`` template is perfectly straightforward:
6868
you'll learn how to do this correctly.
6969

7070
To include the controller, you'll need to refer to it using the standard
71-
string syntax for controllers (i.e. **controllerPath**::**action**):
71+
string syntax for controllers (i.e. **controllerNamespace**::**action**):
7272

7373
.. configuration-block::
7474

templating/formats.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pattern is to do the following::
3030
/**
3131
* @Route("/{slug}")
3232
*/
33-
public function showAction(Request $request, $slug)
33+
public function show(Request $request, $slug)
3434
{
3535
// retrieve the article based on $slug
3636
$article = ...;
@@ -53,7 +53,7 @@ special ``_format`` placeholder in your route definition::
5353
/**
5454
* @Route("/{slug}.{_format}", defaults={"_format": "html"})
5555
*/
56-
public function showAction(Request $request, $slug)
56+
public function show(Request $request, $slug)
5757
{
5858
// ...
5959
}

0 commit comments

Comments
 (0)