Skip to content

Commit e0b8106

Browse files
committed
Merge branch '2.8' into 3.2
* 2.8: [#7907] add some use statements Updating Doctrine syntax for getRepository method Reworded the tip about property_info and Symfony framework remove useless space Add information for enable property_info service Updating doctrine class use [#7909] fix some typos Explained the locateResource() method of HttpKernel Reworded the note about dump() not being available in prod Symfony Installer Instructions for Windows Typo Fixing a typo in the Final Thoughts section Stop recommending the use of "doctrine:generate:entities" Added a help note about translating routes Use a safer code sample for Twig best practices Use the Twig namespaced syntax for all templates Updated the screenshot of deprecation messages
2 parents efc9a0e + 0b78495 commit e0b8106

34 files changed

+195
-189
lines changed
14.2 KB
Loading

best_practices/controllers.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ for the homepage of our app:
9595
9696
namespace AppBundle\Controller;
9797
98+
use AppBundle\Entity\Post;
9899
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
99100
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
100101
@@ -106,7 +107,7 @@ for the homepage of our app:
106107
public function indexAction()
107108
{
108109
$posts = $this->getDoctrine()
109-
->getRepository('AppBundle:Post')
110+
->getRepository(Post::class)
110111
->findLatest();
111112
112113
return $this->render('default/index.html.twig', array(
@@ -170,7 +171,7 @@ manually. In our application, we have this situation in ``CommentController``:
170171
public function newAction(Request $request, $postSlug)
171172
{
172173
$post = $this->getDoctrine()
173-
->getRepository('AppBundle:Post')
174+
->getRepository(Post::class)
174175
->findOneBy(array('slug' => $postSlug));
175176
176177
if (!$post) {

best_practices/security.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ more advanced use-case, you can always do the same security check in PHP:
226226
*/
227227
public function editAction($id)
228228
{
229-
$post = $this->getDoctrine()->getRepository('AppBundle:Post')
229+
$post = $this->getDoctrine()->getRepository(Post::class)
230230
->find($id);
231231
232232
if (!$post) {

best_practices/templates.rst

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,20 @@ Template Locations
3030
Store all your application's templates in ``app/Resources/views/`` directory.
3131

3232
Traditionally, Symfony developers stored the application templates in the
33-
``Resources/views/`` directory of each bundle. Then they used the logical name
34-
to refer to them (e.g. ``AcmeDemoBundle:Default:index.html.twig``).
33+
``Resources/views/`` directory of each bundle. Then they used the Twig namespaced
34+
path to refer to them (e.g. ``@AcmeDemo/Default/index.html.twig``).
3535

3636
But for the templates used in your application, it's much more convenient
3737
to store them in the ``app/Resources/views/`` directory. For starters, this
3838
drastically simplifies their logical names:
3939

40-
================================================= ==================================
41-
Templates Stored inside Bundles Templates Stored in ``app/``
42-
================================================= ==================================
43-
``AcmeDemoBundle:Default:index.html.twig`` ``default/index.html.twig``
44-
``::layout.html.twig`` ``layout.html.twig``
45-
``AcmeDemoBundle::index.html.twig`` ``index.html.twig``
46-
``AcmeDemoBundle:Default:subdir/index.html.twig`` ``default/subdir/index.html.twig``
47-
``AcmeDemoBundle:Default/subdir:index.html.twig`` ``default/subdir/index.html.twig``
48-
================================================= ==================================
40+
============================================ ==================================
41+
Templates Stored inside Bundles Templates Stored in ``app/``
42+
============================================ ==================================
43+
``@AcmeDemo/index.html.twig`` ``index.html.twig``
44+
``@AcmeDemo/Default/index.html.twig`` ``default/index.html.twig``
45+
``@AcmeDemo/Default/subdir/index.html.twig`` ``default/subdir/index.html.twig``
46+
============================================ ==================================
4947

5048
Another advantage is that centralizing your templates simplifies the work
5149
of your designers. They don't need to look for templates in lots of directories
@@ -131,7 +129,7 @@ service in the constructor of the Twig extension:
131129
new \Twig_SimpleFilter(
132130
'md2html',
133131
array($this, 'markdownToHtml'),
134-
array('is_safe' => array('html'))
132+
array('is_safe' => array('html'), 'pre_escape' => 'html')
135133
),
136134
);
137135
}

bundles/best_practices.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,21 @@ The ``composer.json`` file should include at least the following metadata:
422422
In order to make it easier for developers to find your bundle, register it on
423423
`Packagist`_, the official repository for Composer packages.
424424

425+
Resources
426+
---------
427+
428+
If the bundle references any resources (config files, translation files, etc.),
429+
don't use physical paths (e.g. ``__DIR__/config/services.xml``) but logical
430+
paths (e.g. ``@AppBundle/Resources/config/services.xml``).
431+
432+
The logical paths are required because of the bundle overriding mechanism that
433+
lets you override any resource/file of any bundle. See :ref:`http-kernel-resource-locator`
434+
for more details about transforming physical paths into logical paths.
435+
436+
Beware that templates use a simplified version of the logical path shown above.
437+
For example, an ``index.html.twig`` template located in the ``Resources/views/Default/``
438+
directory of the AppBundle, is referenced as ``@App/Default/index.html.twig``.
439+
425440
Learn more
426441
----------
427442

bundles/override.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ How to Override any Part of a Bundle
77
This document is a quick reference for how to override different parts of
88
third-party bundles.
99

10+
.. tip::
11+
12+
The bundle overriding mechanism means that you cannot use physical paths to
13+
refer to bundle's resources (e.g. ``__DIR__/config/services.xml``). Always
14+
use logical paths in your bundles (e.g. ``@AppBundle/Resources/config/services.xml``)
15+
and call the :ref:`locateResource() method <http-kernel-resource-locator>`
16+
to turn them into physical paths when needed.
17+
1018
Templates
1119
---------
1220

components/form.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ is created from the form factory.
418418
->add('dueDate', DateType::class)
419419
->getForm();
420420
421-
return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
421+
return $this->render('@AcmeTask/Default/new.html.twig', array(
422422
'form' => $form->createView(),
423423
));
424424
}

components/http_kernel.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,32 @@ look like this::
742742
// ...
743743
}
744744

745+
.. _http-kernel-resource-locator:
746+
747+
Locating Resources
748+
------------------
749+
750+
The HttpKernel component is responsible of the bundle mechanism used in Symfony
751+
applications. The key feature of the bundles is that they allow to override any
752+
resource used by the application (config files, templates, controllers,
753+
translation files, etc.)
754+
755+
This overriding mechanism works because resources are referenced not by their
756+
physical path but by their logical path. For example, the ``services.xml`` file
757+
stored in the ``Resources/config/`` directory of a bundle called AppBundle is
758+
referenced as ``@AppBundle/Resources/config/services.xml``. This logical path
759+
will work when the application overrides that file and even if you change the
760+
directory of AppBundle.
761+
762+
The HttpKernel component provides a method called :method:`Symfony\\Component\\HttpKernel\\Kernel::locateResource`
763+
which can be used to transform logical paths into physical paths::
764+
765+
use Symfony\Component\HttpKernel\HttpKernel;
766+
767+
// ...
768+
$kernel = new HttpKernel($dispatcher, $resolver);
769+
$path = $kernel->locateResource('@AppBundle/Resources/config/services.xml');
770+
745771
Learn more
746772
----------
747773

components/property_info.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,17 @@ Using PHP reflection, the :class:`Symfony\\Component\\PropertyInfo\\Extractor\\R
348348
provides list, type and access information from setter and accessor methods.
349349
It can also provide return and scalar types for PHP 7+.
350350

351-
This service is automatically registered with the ``property_info`` service in
352-
the Symfony Framework.
351+
.. note::
352+
353+
When using the Symfony framework, this service is automatically registered
354+
when the ``property_info`` feature is enabled:
355+
356+
.. code-block:: yaml
357+
358+
# app/config/config.yml
359+
framework:
360+
property_info:
361+
enabled: true
353362
354363
.. code-block:: php
355364

components/security/authorization.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ on a "remember-me" cookie, or even authenticated anonymously?
110110
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
111111
112112
$anonymousClass = AnonymousToken::class;
113-
$rememberMeClass = RememberMeToken::Class;
113+
$rememberMeClass = RememberMeToken::class;
114114
115115
$trustResolver = new AuthenticationTrustResolver($anonymousClass, $rememberMeClass);
116116

0 commit comments

Comments
 (0)