在 Symfony 控制器中使用容器:调试结果


Use container:debug results in Symfony controller

当你运行app/console container:debug时,你会得到所有已定义服务的列表。我想在我的控制器(扩展Symfony控制器)中使用此列表(仅定义),我想使用该列表来运行并查看我是否可以识别服务。

另一种选择是 $this->get('example.start*') 中的星形加载服务或该 fassion 中的某些内容,有人这样做过吗?我错过了一些文档吗?

谢谢!!祝大家好!

我希望它对你有用...

查看 http://api.symfony.com/2.5/Symfony/Component/DependencyInjection/ContainerBuilder.html 更多数据...

use Symfony'Component'DependencyInjection'Loader'XmlFileLoader;
use Symfony'Component'DependencyInjection'ContainerBuilder;
use Symfony'Component'Config'FileLocator;                      
class   SomeController
{
    public  function   containerDebuglAction()
    {
        print_r( array_keys( $this->getContainerBuilder()->getDefinitions() ) );
    }

    /** 
     * Loads the ContainerBuilder from the cache.                                                                                               
     *      
     * @return ContainerBuilder                                                                                                                 
     *                                                                                                                                          
     * @throws 'LogicException
     */ 
    protected function getContainerBuilder()
    {   
        if (!is_file($cachedFile = $this->get('service_container')->getParameter('debug.container.dump'))) {                                    
            throw new 'LogicException(sprintf('Debug information about the container could not be found. Please clear the cache and try again.')
        }                                                                                                                                       
        $container = new ContainerBuilder();                                                                                                    
        $loader = new XmlFileLoader($container, new FileLocator());                                                                             
        $loader->load($cachedFile);
        return $container;                                                                                                                      
    }       
}