[英]Register Dependency Injection (DI) Extension
为了自动检测DI扩展 ,我尝试遵循文档 ,但在我的项目中,我没有在文件夹中添加Bundle
前缀,我认为我需要手动注册该扩展。
这是我刚刚创建此AppContextExtension
树形文件夹结构
portal/src/Common └── Infrastructure └── Symfony ├── Controller └── DependencyInjection └── AppContextExtension
namespace Portal\Common\Infrastructure\Symfony\DependencyInjection;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Routing\Loader\YamlFileLoader;
class AppContextExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
dump('it work');die;
}
}
因此,任何想法如何将此扩展注册到容器以使dump
? 以及为什么扩展名没有自动注册 !
我认为我们有点徘徊在这里,但是我也许这就是您要求的示例。
# portal/src/Symfony
# could have been portal/src/Portal/Common/Infrastructure/Symfony
# but I got lazy
namespace Portal\Common\Infrastructure\Symfony;
use Portal\Common\Infrastructure\Symfony\DependencyInjection\AppContextExtension;
use Symfony\Component\HttpKernel\Bundle\Bundle;
# I suppose SymfonyBundle might be a better name
class PortalBundle extends Bundle
{
public function getContainerExtension()
{
return new AppContextExtension();
}
}
扩展名
namespace Portal\Common\Infrastructure\Symfony\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
class AppContextExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
dump('it work');die;
}
}
config / bundles.php
Portal\Common\Infrastructure\Symfony\PortalBundle::class => ['all' => true],
composer.json
"autoload": {
"psr-4": {
"App\\": "src/",
"Portal\\Common\\Infrastructure\\Symfony\\": "portal/src/Symfony/"
}
},
所有这些都按预期工作,但是您对autowire的评论使我怀疑您正在尝试完全做其他事情。 Autowire通常用于应用程序定义的服务。 您不会为接线束自动接线。
请享用
实际上,解决方案是将Extension类注入AppKernel内部,因为可以使用Kernel::build()
加载Extensions。
//AppKernel.php
class AppKernel extends Kernel
{
//
protected function build(ContainerBuilder $container)
{
$container->registerExtension(new \Portal\Common\Infrastructure\Symfony\DependencyInjection\AppExtension());
}
感谢@Cerad对我的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.