Bundle 的 Symfony 4 自定义配置 yaml 文件
Symfony 4 Custom config yaml file for Bundle
我正在尝试将一个包转换为 symfony 4,并且需要将我古老的 parameters.yml 更新为现代的 symfony 4 生活方式。 Basicall 捆绑包本身 - 在多个应用程序之间共享 - 应该在 /config/packages/.
下有一个可配置文件
但是我收到这个错误:
(1/1) InvalidArgumentException
There is no extension able to load the configuration for "ptmr" (in /var/www/html/ptmr/pws_ptmrio_dev/PtmrBundle/DependencyInjection/../../config/packages/ptmr.yaml). Looked for namespace "ptmr", found none
/PtmrBundle/DependencyInjection/PtmrExtension.php
<?php
namespace PtmrBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class PtmrExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration(true);
$config = $this->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../../config/packages')
);
$loader->load('ptmr.yaml');
}
}
/PtmrBundle/DependencyInjection/Configuration.php
<?php
namespace PtmrBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
private $debug;
public function __construct($debug = true)
{
$this->debug = (bool) $debug;
}
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('ptmr');
$treeBuilder->getRootNode()
->children()
->arrayNode('twitter')
->children()
->integerNode('client_id')->end()
->scalarNode('client_secret')->end()
->end()
->end() // twitter
->end()
;
return $treeBuilder;
}
}
/config/packages/ptmr.yaml
ptmr:
twitter:
client_id: 123
client_secret: your_secret
--
笔记:
捆绑包本身有效。
我在 composer.json 中将此行添加到 psr-4:
"PtmrBundle\": "PtmrBundle/"
这行到config/routes/annotations.yml
ptmr_bundle:
resource: ../PtmrBundle/Controller/
type: annotation
配置这些行/services.yaml
services:
...
PtmrBundle\:
resource: '../PtmrBundle/*'
exclude: '../PtmrBundle/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
...
PtmrBundle\Controller\:
resource: '../PtmrBundle/Controller'
tags: ['controller.service_arguments']
当然还有PtmrBundle/PtmrBundle.php
<?php
namespace PtmrBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class PtmrBundle extends Bundle
{
}
我正在按照这些说明操作,我确实没有看到任何错误。我错过了什么?交响乐 4.2.
您正试图在 Symfony 发现您的包之前加载您的包的配置。您的包 class 必须先加载和配置,然后才能解析 ptmr
属性.
下的配置
通常,您的包会加载 __DIR__.'/../Resources/config/services.yaml
,其中包含 service
和 parameters
定义。在你的包之外,在你的应用程序 config/
目录中,然后你将在 ptmr
属性.
下加载配置
终于找到答案了。要制作您自己的 config/bundle.yaml
参数文件,只需执行以下操作:
第 1 步: 在您的包中创建一个文件 DependencyInjection/{BundleNameWithoutBundle}Extension.php
,例如对于 MyBundle > MyExtension.php
<?php
namespace MyBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
class MyExtension extends \Symfony\Component\DependencyInjection\Extension\Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('my', $config);
}
}
另见 How to Load Service Configuration inside a Bundle
步骤 2:制作一个配置文件,为您的 .yaml 文件提供架构 DependencyInjection/Configuration.php
<?php
namespace MyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('my');
$treeBuilder->getRootNode()
->children()
->variableNode('project_name')->end()
->end()
;
return $treeBuilder;
}
}
另见 How to Create Friendly Configuration for a Bundle
第 3 步:在 config/my.yaml
中镜像 Configuration.php
my:
project_name: "My Name"
现在参数 my
可用(在 MyExtension Class 中设置)。只需在您的控制器中获取它,例如:
class IndexController extends AbstractController
{
public function indexAction(ParameterBagInterface $parameterBag)
{
...
dump($parameterBag->get('ptmr'));
return $this->render('index.html.twig');
}
}
注意: 大多数 bundle 更进一步并操作 bundle 配置 xml 文件,这超出了像这样的简单问题的范围。关于如何执行此操作的一个简单示例是 KnpPaginatorBundle,对于设置参数来说理解起来并不过分复杂。
个人说明: 在我看来,Symfony 文档过于复杂,应该提供一个简单的示例。你必须了解很多术语,但很难学会,尤其是与其他有据可查的 symfony 章节相比。
我正在尝试将一个包转换为 symfony 4,并且需要将我古老的 parameters.yml 更新为现代的 symfony 4 生活方式。 Basicall 捆绑包本身 - 在多个应用程序之间共享 - 应该在 /config/packages/.
下有一个可配置文件但是我收到这个错误:
(1/1) InvalidArgumentException
There is no extension able to load the configuration for "ptmr" (in /var/www/html/ptmr/pws_ptmrio_dev/PtmrBundle/DependencyInjection/../../config/packages/ptmr.yaml). Looked for namespace "ptmr", found none
/PtmrBundle/DependencyInjection/PtmrExtension.php
<?php
namespace PtmrBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class PtmrExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration(true);
$config = $this->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../../config/packages')
);
$loader->load('ptmr.yaml');
}
}
/PtmrBundle/DependencyInjection/Configuration.php
<?php
namespace PtmrBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
private $debug;
public function __construct($debug = true)
{
$this->debug = (bool) $debug;
}
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('ptmr');
$treeBuilder->getRootNode()
->children()
->arrayNode('twitter')
->children()
->integerNode('client_id')->end()
->scalarNode('client_secret')->end()
->end()
->end() // twitter
->end()
;
return $treeBuilder;
}
}
/config/packages/ptmr.yaml
ptmr:
twitter:
client_id: 123
client_secret: your_secret
-- 笔记: 捆绑包本身有效。
我在 composer.json 中将此行添加到 psr-4:
"PtmrBundle\": "PtmrBundle/"
这行到config/routes/annotations.yml
ptmr_bundle:
resource: ../PtmrBundle/Controller/
type: annotation
配置这些行/services.yaml
services:
...
PtmrBundle\:
resource: '../PtmrBundle/*'
exclude: '../PtmrBundle/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
...
PtmrBundle\Controller\:
resource: '../PtmrBundle/Controller'
tags: ['controller.service_arguments']
当然还有PtmrBundle/PtmrBundle.php
<?php
namespace PtmrBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class PtmrBundle extends Bundle
{
}
我正在按照这些说明操作,我确实没有看到任何错误。我错过了什么?交响乐 4.2.
您正试图在 Symfony 发现您的包之前加载您的包的配置。您的包 class 必须先加载和配置,然后才能解析 ptmr
属性.
通常,您的包会加载 __DIR__.'/../Resources/config/services.yaml
,其中包含 service
和 parameters
定义。在你的包之外,在你的应用程序 config/
目录中,然后你将在 ptmr
属性.
终于找到答案了。要制作您自己的 config/bundle.yaml
参数文件,只需执行以下操作:
第 1 步: 在您的包中创建一个文件 DependencyInjection/{BundleNameWithoutBundle}Extension.php
,例如对于 MyBundle > MyExtension.php
<?php
namespace MyBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
class MyExtension extends \Symfony\Component\DependencyInjection\Extension\Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('my', $config);
}
}
另见 How to Load Service Configuration inside a Bundle
步骤 2:制作一个配置文件,为您的 .yaml 文件提供架构 DependencyInjection/Configuration.php
<?php
namespace MyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('my');
$treeBuilder->getRootNode()
->children()
->variableNode('project_name')->end()
->end()
;
return $treeBuilder;
}
}
另见 How to Create Friendly Configuration for a Bundle
第 3 步:在 config/my.yaml
Configuration.php
my:
project_name: "My Name"
现在参数 my
可用(在 MyExtension Class 中设置)。只需在您的控制器中获取它,例如:
class IndexController extends AbstractController
{
public function indexAction(ParameterBagInterface $parameterBag)
{
...
dump($parameterBag->get('ptmr'));
return $this->render('index.html.twig');
}
}
注意: 大多数 bundle 更进一步并操作 bundle 配置 xml 文件,这超出了像这样的简单问题的范围。关于如何执行此操作的一个简单示例是 KnpPaginatorBundle,对于设置参数来说理解起来并不过分复杂。
个人说明: 在我看来,Symfony 文档过于复杂,应该提供一个简单的示例。你必须了解很多术语,但很难学会,尤其是与其他有据可查的 symfony 章节相比。