Car*_*los 2 symfony symfony-3.1
我正在开发一个依赖于另一个捆绑包的捆绑包。
为了处理尚未安装基本捆绑软件的情况,我想在控制器内部执行“ bundle_exists()”功能。
问题是:如何获得已安装捆绑软件的列表,或者如何检查捆绑软件的名称(最终还是版本)。
谢谢。
除了@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.'
            );
        }
    }
}
您的课程需要访问容器对象(通过扩展或DI)。
那你就可以做;
$this->container->getParameter('kernel.bundles');
这将为您提供已安装的捆绑软件列表。
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.
| 归档时间: | 
 | 
| 查看次数: | 1564 次 | 
| 最近记录: |