symfony 3.1检查软件包是否已安装

Car*_*los 2 symfony symfony-3.1

我正在开发一个依赖于另一个捆绑包的捆绑包。

为了处理尚未安装基本捆绑软件的情况,我想在控制器内部执行“ bundle_exists()”功能。

问题是:如何获得已安装捆绑软件的列表,或者如何检查捆绑软件的名称(最终还是版本)。

谢谢。

Wou*_*r J 7

除了@Rooneyl的答案:

进行此类检查的最佳位置是DI扩展名(例如AcmeDemoExtension)内。一旦构建容器并将其转储到高速缓存,将执行此操作。无需在每个请求上都检查此类内容(容器在进行缓存时不会更改),只会减慢缓存的速度。

// ...
class AcmeDemoExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $bundles = $container->getParameter('bundles');
        if (!isset($bundles['YourDependentBundle'])) {
            throw new \InvalidArgumentException(
                'The bundle ... needs to be registered in order to use AcmeDemoBundle.'
            );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Roo*_*eyl 5

您的课程需要访问容器对象(通过扩展或DI)。
那你就可以做;

$this->container->getParameter('kernel.bundles');
Run Code Online (Sandbox Code Playgroud)

这将为您提供已安装的捆绑软件列表。

Update;
If you are in a controller that extends the Symfony\Bundle\FrameworkBundle\Controller\Controller or in a command class that extends Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand, you can just get the parameter.

$this->getParameter('kernel.bundles').

Else @Wouter J's answer is your best answer.

  • 扩展声音真的很吓人,最好注入参数而不是注入整个容器。 (2认同)