亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Symfony 6 parent controller to child controller dependency injection
P粉208469050
P粉208469050 2023-08-17 20:19:03
0
1
678
<p>I have a base controller class that contains some utility methods that all child controllers use. Currently it has 3 dependencies, but there may be more in the future. So whenever I want to add a dependency to a child controller, I now have an issue where I think there are too many directives for dependency injection. </p> <pre class="brush:php;toolbar:false;">abstract class BaseController extends AbstractController { public function __construct( protected readonly SerializerInterface $serializer, protected readonly ValidatorInterface $validator, private readonly ResponseGenerator $responseGenerator, ) { } ... } class ChildController extends BaseController { // All parent class injections are required in all child classes. public function __construct( SerializerInterface $serializer, ValidatorInterface $validator, ResponseGenerator $responseGenerator, private readonly SomeRepository $someRepository, ...insert any other child controller-specific dependencies here. ) { parent::__construct($serializer, $validator, $responseGenerator); } ... }</pre> <p>I tried using <code>$this->container->get('serializer')</code> in the base controller, but that didn't work because <code>AbstractController: :$container</code> is defined through injection, but has no constructor, so <code>parent::__construct()</code> cannot be called. Also, it doesn't give me a <code>validator</code>, so even if it works, it only solves part of the problem. </p> <p>I tried looking for properties that I could use, like </p> <pre class="brush:php;toolbar:false;">abstract class BaseController extends AbstractController { #[Inject] protected readonly SerializerInterface $serializer; #[Inject] protected readonly ValidatorInterface $validator;</pre> <p>But nothing similar was found (PHP-DI has it, but Symfony doesn't). </p> <p>Is there a way to somehow eliminate duplicate dependencies in child controllers? </p>
P粉208469050
P粉208469050

reply all(1)
P粉464208937

What you need is to be called a Service Subscriber

In Symfony, when controllers inherit AbstractController, they are service subscribers, which means they are injected with a library containing some common services (such as twig, serializers, form builders, etc. ) small container.

If you want some "common" services that your child controllers will use, you can extend the list by overriding getSubscribedServices() in the parent controller. Or if your controller does not inherit the default controller provided by Symfony, you just need to implement your own controller:

If your controller is a service (which I guess it already is), Symfony will use setter injection to inject the container into your controller.

The code will look like this:

<?php

use Symfony\Contracts\Service\ServiceSubscriberInterface;


class ParentController implement ServiceSubscriberInterface {
    protected ContainerInterface $container;
    public function setContainer(ContainerInterface) { $this->container = $container; } 

    public static function getSubscribedServices() {
         // 這是靜態(tài)的,所以Symfony可以在不實例化控制器的情況下“看到”所需的服務(wù)。
         // 在這里定義一些常見的服務(wù),一個示例在Symfony的AbstractController中
    }
}

class ChildController extends ParentController {
    // 使用自定義DI來為子控制器提供服務(wù)。

    public function indexAction {
        // 你可以使用$this->container->get(...)來獲取服務(wù)
    }

}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template