1

我想根据存储在数据库中的数据禁用一些捆绑包。我知道我可以根据环境做到这一点:

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle()             );

        // Environment based bundles
        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }
}

但是,这显然不能回答这个问题,我可以将服务注入AppKernel自身吗?或者我可以将它们注入捆绑包本身并以某种方式禁用它吗?

4

1 回答 1

2

如果我是你,我会做以下事情

  1. 创建一个简单的包,它将处理对数据库的请求和所有这些东西
  2. 将此捆绑包注册到app/AppKernel
  3. 创建YourKernel类并扩展它app/AppKernel
  4. “注入”YourKernelweb/app.php
  5. YourKernel重新定义boot()方法中,您将根据返回值获得捆绑和引导所需的列表。
于 2013-09-06T13:43:25.243 回答