From 89e38ef8b5f221ffcae000bf5991e3085e99b302 Mon Sep 17 00:00:00 2001 From: Tom Maaswinkel Date: Fri, 5 Jan 2018 11:45:49 +0100 Subject: [PATCH 1/4] Removed invalid examples Templating isn't a service in Symfony 4+ (even after `composer req templating`), so probably we're looking for the twig service. The Router service isn't public and can therefor not be called from the container: ``` Information for Service "router.default" ======================================== ---------------- ----------------------------------------------- Option Value ---------------- ----------------------------------------------- Service ID router.default Class Symfony\Bundle\FrameworkBundle\Routing\Router Tags - Calls setConfigCacheFactory Public no Synthetic no Lazy no Shared yes Abstract no Autowired no Autoconfigured no ---------------- ----------------------------------------------- ``` --- controller.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/controller.rst b/controller.rst index 102ca934e3f..0477fa5001e 100644 --- a/controller.rst +++ b/controller.rst @@ -299,9 +299,7 @@ If you extend the base ``Controller`` class, you can access :ref:`public service via the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::get` method. Here are several common services you might need:: - $templating = $this->get('templating'); - - $router = $this->get('router'); + $twig = $this->get('twig'); $mailer = $this->get('mailer'); From 18e6d9ec66d6ec54e94754f04b1deaf9fdb68cee Mon Sep 17 00:00:00 2001 From: Tom Maaswinkel Date: Fri, 5 Jan 2018 19:26:27 +0100 Subject: [PATCH 2/4] Removed "Accessing the Container Directly" Removed "Accessing the Container Directly" section, as this way of work is currently deprecated. --- controller.rst | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/controller.rst b/controller.rst index 0477fa5001e..dec8b34dd99 100644 --- a/controller.rst +++ b/controller.rst @@ -290,31 +290,6 @@ in your controllers. For more information about services, see the :doc:`/service_container` article. -.. _controller-access-services-directly: - -Accessing the Container Directly -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -If you extend the base ``Controller`` class, you can access :ref:`public services ` -via the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::get` -method. Here are several common services you might need:: - - $twig = $this->get('twig'); - - $mailer = $this->get('mailer'); - - // you can also fetch parameters - $someParameter = $this->getParameter('some_parameter'); - -If you receive an error like: - -.. code-block:: text - - You have requested a non-existent service "my_service_id" - -Check to make sure the service exists (use :ref:`debug:container `) -and that it's :ref:`public `. - .. index:: single: Controller; Managing errors single: Controller; 404 pages From f9091b0f59ebd610e06b0eb1598bc510870793d0 Mon Sep 17 00:00:00 2001 From: Tom Maaswinkel Date: Fri, 5 Jan 2018 21:35:59 +0100 Subject: [PATCH 3/4] Updated documentation to reflect best practice of using Dependency Injection over Container. Includes changes from PR #8985 --- best_practices/security.rst | 26 +++++++++++++++++++++----- doctrine/multiple_entity_managers.rst | 13 ++++++++++++- routing.rst | 2 +- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/best_practices/security.rst b/best_practices/security.rst index d2bb31f09b5..5e5d78e90aa 100644 --- a/best_practices/security.rst +++ b/best_practices/security.rst @@ -238,12 +238,20 @@ more advanced use-case, you can always do the same security check in PHP: // equivalent code without using the "denyAccessUnlessGranted()" shortcut: // // use Symfony\Component\Security\Core\Exception\AccessDeniedException; + // use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface + // + // ... + // + // public function __construct(AuthorizationCheckerInterface $authorizationChecker) { + // $this->authorizationChecker = $authorizationChecker; + // } + // // ... // - // if (!$this->get('security.authorization_checker')->isGranted('edit', $post)) { + // if (!$this->authorizationChecker->isGranted('edit', $post)) { // throw $this->createAccessDeniedException(); // } - + // // ... } @@ -357,14 +365,22 @@ via the even easier shortcut in a controller: $this->denyAccessUnlessGranted('edit', $post); - // or without the shortcut: - // // use Symfony\Component\Security\Core\Exception\AccessDeniedException; + // use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface + // // ... // - // if (!$this->get('security.authorization_checker')->isGranted('edit', $post)) { + // public function __construct(AuthorizationCheckerInterface $authorizationChecker) { + // $this->authorizationChecker = $authorizationChecker; + // } + // + // ... + // + // if (!$this->authorizationChecker->isGranted('edit', $post)) { // throw $this->createAccessDeniedException(); // } + // + // ... } Learn More diff --git a/doctrine/multiple_entity_managers.rst b/doctrine/multiple_entity_managers.rst index 78ddd96006d..274107799c9 100644 --- a/doctrine/multiple_entity_managers.rst +++ b/doctrine/multiple_entity_managers.rst @@ -231,11 +231,22 @@ the default entity manager (i.e. ``default``) is returned:: // ... + use Doctrine\ORM\EntityManagerInterface; + class UserController extends Controller { + protected $em; + + public function __construct(EntityManagerInterface $em) + { + $this->em = $em; + } + public function indexAction() { - // All 3 return the "default" entity manager + // All 4 return the "default" entity manager, though using + // dependency injection is a best practice + $em = $this->em; $em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager('default'); $em = $this->get('doctrine.orm.default_entity_manager'); diff --git a/routing.rst b/routing.rst index 99a510504e9..b1a14956458 100644 --- a/routing.rst +++ b/routing.rst @@ -576,7 +576,7 @@ Generating URLs with Query Strings The ``generate()`` method takes an array of wildcard values to generate the URI. But if you pass extra ones, they will be added to the URI as a query string:: - $this->get('router')->generate('blog', array( + $this->router->generate('blog', array( 'page' => 2, 'category' => 'Symfony' )); From 3492028a0e583e0f7167572ce8e31b64d278e9f2 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 7 Jan 2018 13:28:42 +0100 Subject: [PATCH 4/4] Use controller method injection instead of the constructor --- doctrine/multiple_entity_managers.rst | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/doctrine/multiple_entity_managers.rst b/doctrine/multiple_entity_managers.rst index 274107799c9..bc958e0d0f9 100644 --- a/doctrine/multiple_entity_managers.rst +++ b/doctrine/multiple_entity_managers.rst @@ -235,18 +235,10 @@ the default entity manager (i.e. ``default``) is returned:: class UserController extends Controller { - protected $em; - - public function __construct(EntityManagerInterface $em) - { - $this->em = $em; - } - - public function indexAction() + public function indexAction(EntityManagerInterface $em) { - // All 4 return the "default" entity manager, though using - // dependency injection is a best practice - $em = $this->em; + // These methods also return the default entity manager, but it's preferred + // to get it by inyecting EntityManagerInterface in the action method $em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager('default'); $em = $this->get('doctrine.orm.default_entity_manager'); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy