[英]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
-- 注意: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
{
}
我正在按照這些說明進行操作,我真的沒有看到任何錯誤。 我錯過了什么? 交響樂 4.2。
我終於找到了答案。 要制作您自己的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);
}
}
另請參閱如何在捆綁包中加載服務配置
第 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;
}
}
另請參閱如何為捆綁包創建友好配置
第 3 步:在您的config/my.yaml
中鏡像Configuration.php
my:
project_name: "My Name"
現在參數my
可用(在 MyExtension 類中設置)。 只需在您的控制器中獲取它,例如:
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 知道你的包之前加載你的包的配置。 在解析ptmr
屬性下的配置之前,必須先加載和配置您的捆綁包類。
通常,您的包會加載__DIR__.'/../Resources/config/services.yaml
,其中包含service
和parameters
定義。 在你的包之外,在你的應用程序config/
目錄中,你將在ptmr
屬性下加載配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.