Php 将内核访问到bundle扩展类中
我试图增强我的一个包扩展,它加载我的YAML配置文件,我必须访问内核类才能使用方法Php 将内核访问到bundle扩展类中,php,symfony,Php,Symfony,我试图增强我的一个包扩展,它加载我的YAML配置文件,我必须访问内核类才能使用方法locateResource() 目标是加载资源文件而不使用: __DIR__ . '/../Resources/config' 扩展类如下所示: class InnovaPathExtension extends Extension { /** * Base path to the config directory of the bundle * @var unknown
locateResource()
目标是加载资源文件而不使用:
__DIR__ . '/../Resources/config'
扩展类如下所示:
class InnovaPathExtension extends Extension
{
/**
* Base path to the config directory of the bundle
* @var unknown
*/
const CONFIG_PATH = '@InnovaPathBundle/Resources/config/';
/**
* List of needed config files
* @var array
*/
protected $filesToLoad = array (
// Services
'services/services.yml',
'services/listeners.yml',
'services/managers.yml',
'services/controllers.yml',
// Parameters
'parameters.yml',
);
/**
* @see \Symfony\Component\DependencyInjection\Extension\ExtensionInterface::load()
*/
public function load(array $configs, ContainerBuilder $container)
{
foreach ($this->filesToLoad as $file) {
// Check if resources exists
try {
$resource = static::CONFIG_PATH . $file;
$filePath = $container->get('kernel')->locateResource($resource);
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException(sprintf('Unable to load "%s"', $resource), 0, $e);
}
// TODO : Load resource
}
return $this;
}
}
指令$container->get('kernel')
引发异常“服务定义“kernel”不存在”
如果我检查使用$container->getServiceIds()
注册的所有服务,我找到的唯一一个服务是服务\u container
没问题,我抓取服务容器并使用$container->get('service\u container')->getServiceIds()
检查服务。。。我只得到一项服务:服务\u容器
我不明白为什么。。。这是一种例外吗?推荐的方法是:
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
foreach ($this->filesToLoad as $file) {
$loader->load($file);
}
}
不建议采用其他方式
请参见是的,这是我在开始增强之前所做的。我将使用
YamlFileLoader
和FileLocator
,但我想在构造函数调用中删除对\uuuu DIR\uuuu
的引用。可能吧,但如果是关于SensioInsight的通知,这是一个肯定的错误。在这种状态下,内核不会像您所检查的那样加载,并且use DIR不是一个糟糕的编码解决方案,而是预期的解决方案。检查Sensio、KNPLAB、Liip、FOS等的所有主要捆绑包。这是正确的选择!好啊我不知道locateResource
方法,我想我可以在扩展中使用它:(。