8000 feature #60081 [FrameworkBundle] Enable controller service with `#[Ro… · symfony/symfony@981b556 · GitHub
[go: up one dir, main page]

Skip to content

Commit 981b556

Browse files
committed
feature #60081 [FrameworkBundle] Enable controller service with #[Route] attribute (GromNaN)
This PR was merged into the 7.3 branch. Discussion ---------- [FrameworkBundle] Enable controller service with `#[Route]` attribute | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT The `#[AsController]` attribute has 2 purposes: - Add the tag ` controller.service_argument` that configures the service and service argument injection ([RegisterControllerArgumentLocatorsPass](https://github.com/symfony/symfony/blob/79ea49c772ce4b39f414cde5648ad347c3bbcfd7/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php#L56)) - Allow the class in the controller resolver ([#52471](#52471)) In this PR, I propose to add the tag `argument_resolver.service` on services when the class has the `#[Route]` attribute. Removing the need for `#[AsController]` on classes that use the `#[Route]` attribute. I assume that if there is a route, it is a controller. Diff (from the [docs](https://symfony.com/doc/7.2/controller/service.html)): ```diff namespace App\Controller; use Symfony\Component\HttpFoundation\Response; - use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\Routing\Attribute\Route; - #[AsController] class HelloController { #[Route('/hello', name: 'hello', methods: ['GET'])] public function index(): Response { // ... } } ``` Commits ------- 2e59467 Enable controller service with #[Route] attribute instead of #[AsController]
2 parents f6b63a2 + 2e59467 commit 981b556

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ CHANGELOG
1919
* Add DI alias from `ServicesResetterInterface` to `services_resetter`
2020
* Add `methods` argument in `#[IsCsrfTokenValid]` attribute
2121
* Allow configuring the logging channel per type of exceptions
22+
* Enable service argument resolution on classes that use the `#[Route]` attribute,
23+
the `#[AsController]` attribute is no longer required
2224

2325
7.2
2426
---

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
use Symfony\Component\RateLimiter\Storage\CacheStorage;
165165
use Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer;
166166
use Symfony\Component\RemoteEvent\RemoteEvent;
167+
use Symfony\Component\Routing\Attribute\Route;
167168
use Symfony\Component\Scheduler\Attribute\AsCronTask;
168169
use Symfony\Component\Scheduler\Attribute\AsPeriodicTask;
169170
use Symfony\Component\Scheduler\Attribute\AsSchedule;
@@ -429,7 +430,7 @@ public function load(array $configs, ContainerBuilder $container): void
429430
}
430431
$loggers[$exception['log_channel']] = new Reference('monolog.logger.'.$exception['log_channel'], ContainerInterface::NULL_ON_INVALID_REFERENCE);
431432
}
432-
433+
433434
$exceptionListener
434435
->replaceArgument(3, $config['exceptions'])
435436
->setArgument(4, $loggers)
@@ -739,6 +740,9 @@ public function load(array $configs, ContainerBuilder $container): void
739740
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
740741
$definition->addTag('controller.service_arguments');
741742
});
743+
$container->registerAttributeForAutoconfiguration(Route::class, static function (ChildDefinition $definition, Route $attribute, \ReflectionClass|\ReflectionMethod $reflection): void {
744+
$definition->addTag('controller.service_arguments');
745+
});
742746
$container->registerAttributeForAutoconfiguration(AsRemoteEventConsumer::class, static function (ChildDefinition $definition, AsRemoteEventConsumer $attribute): void {
743747
$definition->addTag('remote_event.consumer', ['consumer' => $attribute->name]);
744748
});

0 commit comments

Comments
 (0)
0