简体   繁体   English

如何在 Sonata Admin 类中获取容器实例?

[英]How to get container instance in sonata Admin class?

I need to parse some configurations parameters from my config.yml such as enabled languages.我需要从我的 config.yml 解析一些配置参数,例如启用的语言。 But when i try to do that by using the normal symfony method: $this->container->get('my_params');但是当我尝试使用普通的 symfony 方法来做到这一点时: $this->container->get('my_params'); it fails because my admin class extends Sonata\\AdminBundle\\Admin\\Admin class which does not extend the Symfony\\Component\\DependencyInjection\\ContainerAware class.它失败是因为我的管理类扩展了Sonata\\AdminBundle\\Admin\\Admin类,它没有扩展Symfony\\Component\\DependencyInjection\\ContainerAware类。

Please, How to get the container inside the sonata Admin class ?请问,如何在奏鸣曲管理类中获取容器?

Now i'm resolving this problem by overriding the sonata Admin Class to make it extends the ContainerAware.现在我通过覆盖奏鸣曲管理类来解决这个问题,使其扩展 ContainerAware。

probably already resolved, because its an old question, but just for reference, the container is already available in admin class using the configuration pool... 可能已经解决了,因为它是一个老问题,但仅供参考,容器已在管理类中使用配置池...

$this->getConfigurationPool()->getContainer();

Of course, it is better practice to inject services in the admin class, but, like in the controllers. 当然,最好在admin类中注入服务,但是,就像在控制器中一样。 why would someone, take the time to configure setter injection if already has the container available? 为什么有人,如果已经有容器可用,请花时间配置setter注入?

Add in your Admin class 添加您的Admin类

/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
private $container;

public function setContainer (\Symfony\Component\DependencyInjection\ContainerInterface $container) {
    $this->container = $container;
}

And add calls in services configuration(configuration can be specified in YAML, XML or PHP): 并在服务配置中添加调用(可以在YAML,XML或PHP中指定配置):

YAML YAML

calls:
    - [ setContainer, [ @service_container ] ]

XML XML

 <call method="setContainer">
     <argument type="service" id="service_container" />
 </call>

Now you can using the normal symfony method: $this->container->get() 现在你可以使用普通的symfony方法: $this->container->get()

For more information see Service Container documentation 有关更多信息,请参阅Service Container文档

Here is the best way to use services in sonata admin classes: 以下是在奏鸣曲管理类中使用服务的最佳方法:

Just inject the needed service using setter injection . 只需使用setter注入注入所需的服务。 Constructor injections are not allowed in this case, because you would have to override the constructor of the parent class. 在这种情况下,不允许使用构造函数注入,因为您必须覆盖父类的构造函数。 The constructor of the parent class accepts only 3 parameters, so you can not add another one. 父类的构造函数只接受3个参数,因此您无法添加另一个参数。

The solution is: 解决方案是:

<!-- file: services.xml -->

 <service id="skonsoft.znata.admin.keyword" class="%skonsoft.znata.admin.keyword.class%">
            <tag name="sonata.admin" manager_type="orm" group="Keyword" label="Keyword"/>
            <argument />
            <argument>%skonsoft.znata.admin.keyword.entity.class%</argument>
            <argument>SonataAdminBundle:CRUD</argument>
            <call method="setTranslationDomain">
                <argument>SkonsoftZnataBundle</argument>
            </call>

            <!-- here you inject needed services or parameters -->
            <call method="setEnabledLocales">
                <argument>%skonsoft_znata.locales%</argument>
            </call>
        </service>

Then, just add a public method in your admin class called setEnabledLocales . 然后,只需在管理类中添加一个名为setEnabledLocales的公共方法。

/* file: MyclassAdmin.php */

 public function setEnabledLocales($locales){
    $this->enabedLocales = $locales;
}

Take a look at: 看一眼:

Service Container documentation 服务容器文档

In your particular situation, it might be wise to have a custom AdminController class set the parameters of your Admin class. 在您的特定情况下,让自定义AdminController类设置Admin类的参数可能是明智之举。 This would adhere to the MVC pattern and save you from changing vendor bundles. 这将遵循MVC模式,并使您免于更改供应商包。 If you don't know how to do this, please update your question and I'll explain in more detail. 如果您不知道如何操作,请更新您的问题,我会更详细地解释。

以下是liip_imagine服务的示例:

 $cacheManager = $this->getConfigurationPool()->getContainer()->get('liip_imagine.cache.manager');

The method getContainer() of the Admin Pool has been deprecated since Sonata Admin 3.77.0 and has been removed in v4.x. Admin Pool 的 getContainer() 方法自 Sonata Admin 3.77.0 起已被弃用,并已在 v4.x 中删除。

You could achieve this with dependency injection in sonata Admin class (v4.x).您可以通过 Sonata Admin 类 (v4.x) 中的依赖注入来实现这一点。

For example, I've injected ParameterBagInterface to get application params in my sonata admin.例如,我已注入 ParameterBagInterface 以在我的奏鸣曲管理中获取应用程序参数。

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class ProtocolAdmin extends AbstractAdmin
{
    private $params;

    public function __construct(string $code, string $class, string $baseControllerName, ParameterBagInterface $params)
    {
        parent::__construct($code, $class, $baseControllerName);
        $this->params = $params;
    }
  
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM