Skip to content

Commit bf1d590

Browse files
committed
feature #42442 [FrameworkBundle] Deprecate AbstractController::get() and has() (fabpot)
This PR was merged into the 5.4 branch. Discussion ---------- [FrameworkBundle] Deprecate AbstractController::get() and has() | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | n/a Controllers extending `AbstractController` have access to the services registered for the shortcuts defined in `AbstractController` via `get()` and `has()`. These methods can make developers think that they have access to the whole container, which is not true. Moreover, the limited set of services are precisely the ones needed for the shortcuts, so probably not the ones people would need anyway. I propose to deprecate these methods and advocate using method/constructor injection instead. If people want to still use the container, they can access it via the `$this->container` property (useful if they extend `getSubscribedServices()` for instance). Commits ------- 66a81eadad [FrameworkBundle] Deprecate AbstractController::get() and has()
2 parents 163e693 + b529455 commit bf1d590

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* Add autowiring alias for `HttpCache\StoreInterface`
88
* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
99
* Deprecate the public `profiler` service to private
10-
* Deprecate `getDoctrine()` and `dispatchMessage()` in `AbstractController`, use method/constructor injection instead
10+
* Deprecate `get()`, `has()`, `getDoctrine()`, and `dispatchMessage()` in `AbstractController`, use method/constructor injection instead
1111

1212
5.3
1313
---

Controller/AbstractController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,39 @@ public static function getSubscribedServices()
9696
'session' => '?'.SessionInterface::class,
9797
'security.authorization_checker' => '?'.AuthorizationCheckerInterface::class,
9898
'twig' => '?'.Environment::class,
99-
'doctrine' => '?'.ManagerRegistry::class,
99+
'doctrine' => '?'.ManagerRegistry::class, // to be removed in 6.0
100100
'form.factory' => '?'.FormFactoryInterface::class,
101101
'security.token_storage' => '?'.TokenStorageInterface::class,
102102
'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,
103103
'parameter_bag' => '?'.ContainerBagInterface::class,
104-
'message_bus' => '?'.MessageBusInterface::class,
105-
'messenger.default_bus' => '?'.MessageBusInterface::class,
104+
'message_bus' => '?'.MessageBusInterface::class, // to be removed in 6.0
105+
'messenger.default_bus' => '?'.MessageBusInterface::class, // to be removed in 6.0
106106
];
107107
}
108108

109109
/**
110110
* Returns true if the service id is defined.
111+
*
112+
* @deprecated since 5.4, use method or constructor injection in your controller instead
111113
*/
112114
protected function has(string $id): bool
113115
{
116+
trigger_deprecation('symfony/framework-bundle', '5.4', 'Method "%s()" is deprecated, use method or constructor injection in your controller instead.', __METHOD__);
117+
114118
return $this->container->has($id);
115119
}
116120

117121
/**
118122
* Gets a container service by its id.
119123
*
120124
* @return object The service
125+
*
126+
* @deprecated since 5.4, use method or constructor injection in your controller instead
121127
*/
122128
protected function get(string $id): object
123129
{
130+
trigger_deprecation('symfony/framework-bundle', '5.4', 'Method "%s()" is deprecated, use method or constructor injection in your controller instead.', __METHOD__);
131+
124132
return $this->container->get($id);
125133
}
126134

0 commit comments

Comments
 (0)