Skip to content

Commit f9091b0

Browse files
author
Tom Maaswinkel
committed
Updated documentation to reflect best practice of using Dependency Injection over Container. Includes changes from PR symfony#8985
1 parent 18e6d9e commit f9091b0

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

best_practices/security.rst

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,20 @@ more advanced use-case, you can always do the same security check in PHP:
238238
// equivalent code without using the "denyAccessUnlessGranted()" shortcut:
239239
//
240240
// use Symfony\Component\Security\Core\Exception\AccessDeniedException;
241+
// use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
242+
//
243+
// ...
244+
//
245+
// public function __construct(AuthorizationCheckerInterface $authorizationChecker) {
246+
// $this->authorizationChecker = $authorizationChecker;
247+
// }
248+
//
241249
// ...
242250
//
243-
// if (!$this->get('security.authorization_checker')->isGranted('edit', $post)) {
251+
// if (!$this->authorizationChecker->isGranted('edit', $post)) {
244252
// throw $this->createAccessDeniedException();
245253
// }
246-
254+
//
247255
// ...
248256
}
249257
@@ -357,14 +365,22 @@ via the even easier shortcut in a controller:
357365
358366
$this->denyAccessUnlessGranted('edit', $post);
359367
360-
// or without the shortcut:
361-
//
362368
// use Symfony\Component\Security\Core\Exception\AccessDeniedException;
369+
// use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
370+
//
363371
// ...
364372
//
365-
// if (!$this->get('security.authorization_checker')->isGranted('edit', $post)) {
373+
// public function __construct(AuthorizationCheckerInterface $authorizationChecker) {
374+
// $this->authorizationChecker = $authorizationChecker;
375+
// }
376+
//
377+
// ...
378+
//
379+
// if (!$this->authorizationChecker->isGranted('edit', $post)) {
366380
// throw $this->createAccessDeniedException();
367381
// }
382+
//
383+
// ...
368384
}
369385
370386
Learn More

doctrine/multiple_entity_managers.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,22 @@ the default entity manager (i.e. ``default``) is returned::
231231

232232
// ...
233233

234+
use Doctrine\ORM\EntityManagerInterface;
235+
234236
class UserController extends Controller
235237
{
238+
protected $em;
239+
240+
public function __construct(EntityManagerInterface $em)
241+
{
242+
$this->em = $em;
243+
}
244+
236245
public function indexAction()
237246
{
238-
// All 3 return the "default" entity manager
247+
// All 4 return the "default" entity manager, though using
248+
// dependency injection is a best practice
249+
$em = $this->em;
239250
$em = $this->getDoctrine()->getManager();
240251
$em = $this->getDoctrine()->getManager('default');
241252
$em = $this->get('doctrine.orm.default_entity_manager');

routing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Generating URLs with Query Strings
576576
The ``generate()`` method takes an array of wildcard values to generate the URI.
577577
But if you pass extra ones, they will be added to the URI as a query string::
578578

579-
$this->get('router')->generate('blog', array(
579+
$this->router->generate('blog', array(
580580
'page' => 2,
581581
'category' => 'Symfony'
582582
));

0 commit comments

Comments
 (0)