首页 > 解决方案 > 自定义包中的 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;

}

}

标签: symfony

解决方案


所以我不得不将我的配置参数设置为 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;

    }

推荐阅读