symfony - 自定义包中的 symfony 内核接口
问题描述
我正在使用 symfony4。我按照本教程创建自定义捆绑包。一切正常。从现在开始,我想在我的包中使用 KernelInterface。如何在我的包中添加一些服务(如内核)?
我试图在我的 bundle services.yml 中添加内核
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="lilworks.youtubedl" class="Lilworks\YoutubeDlBundle\LilworksYoutubeDl" public="true">
<argument type="service" id="kernel" />
</service>
<service id="Lilworks\YoutubeDlBundle\LilworksYoutubeDl" alias="lilworks.youtubedl" public="true"/>
</services>
<?php
namespace Lilworks\YoutubeDlBundle;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
class LilworksYoutubeDl
{
private $foo;
private $bar;
private $kernel;
public function __construct($foo,$bar,KernelInterface $kernelInterface)
{
$this->foo = $foo;
$this->bar = $bar;
$this->kernel = $kernelInterface;
}
}
解决方案
所以我不得不将我的配置参数设置为 1 和 2,并将 0 留给想要的服务
class LilworksYoutubeDlExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
$definition = $container->getDefinition('lilworks.youtubedl');
$definition->setArgument(1, $config['foo']);
$definition->setArgument(2, $config['bar']);
}
public function getAlias()
{
return 'lilworks_youtubedl';
}
}
在我的 services.yml
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="lilworks.youtubedl" class="Lilworks\YoutubeDlBundle\LilworksYoutubeDl" public="true">
<argument type="service" id="kernel" />
</service>
<service id="Lilworks\YoutubeDlBundle\LilworksYoutubeDl" alias="lilworks.youtubedl" public="true"/>
</services>
</container>
并且在捆绑包的构造函数中
class LilworksYoutubeDl
{
private $foo;
private $bar;
private $kernel;
public function __construct(KernelInterface $kernelInterface,$foo,$bar)
{
$this->foo = $foo;
$this->bar = $bar;
$this->kernel = $kernelInterface;
}
推荐阅读
- javascript - 在联合js中绘制样式圈
- amazon-web-services - 可以使用 API 代替 SDK 吗?
- asp.net-core - Build error of ASP.NET Core - "...current settings, version 2.1.0-preview3-26411-06 would be used instead"
- r - R - R6 class as a rigid "read-only" data structure
- javascript - 错误:无法读取未定义的属性“顶部”
- vb.net - VB.NET System.StackOverflowException After Adding Redis
- python - 我可以从 HDF5 数据集中删除元素吗?
- java - remember username and password checkbox
- c++ - Visual Studio 15.7 Update: change in constructor inheritance
- javascript - Pubnub sending messages multiple times