php - Symfony 4 Bundle 的自定义配置 yaml 文件
问题描述
我正在尝试将捆绑包转换为 symfony 4,并且需要将我古老的 parameters.yml 更新为现代 symfony 4 的生活方式。基本上,捆绑包本身——在多个应用程序之间共享——应该在 /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
-- 注意:Bundle 本身可以工作。
我将此行添加到composer.json中的 psr-4 :
"PtmrBundle\\": "PtmrBundle/"
这行到 config/routes/ annotations.yml
ptmr_bundle:
resource: ../PtmrBundle/Controller/
type: annotation
这些行到 config/ 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
{
}
我正在按照这些说明进行操作,并且我确实没有看到任何错误。我错过了什么?Symfony 4.2。
解决方案
你试图在 Symfony 知道你的 bundle 之前为你的 bundle 加载配置。必须先加载和配置您的捆绑包类,然后才能解析该ptmr
属性下的配置。
通常,您的捆绑包将加载__DIR__.'/../Resources/config/services.yaml
包含service
和parameters
定义的内容。在您的包之外,在您的应用程序config/
目录中,您将在ptmr
属性下加载配置。
推荐阅读
- android - 菜单项不会在旋转时被破坏 - kotlin
- angular - 如何有条件地将文本与 ngStyle 对齐
- vba - 存在多个时选择收件箱
- framer-motion - 使用共享布局制作动画时,Framer Motion 文本会失真
- apache - 将特定文件夹重定向到子域但没有文件夹名称
- javascript - 如何在页面加载前从 API 获取数据到动态 API URL(空值问题)
- java - 在 MySQL 数据库中已有数据模型时创建实体
- c# - 在 ElapsedEventHandler 中获取日期和时间
- r - 如何在 R 中呈现 pdf 报告?
- swift - Web3Swift 与自定义 ABI 的交互