gpt4 book ai didi

php - 在 Symfony 中创建配置

转载 作者:可可西里 更新时间:2023-11-01 13:41:11 25 4
gpt4 key购买 nike

几个小时以来,我一直在努力做你能想象到的最简单的事情,但它就是行不通。我已经阅读了大量的 stackoverflow 问题,阅读了关于配置文件的完整 Symfony 文档以及我阅读的每篇文章或其他信息,它变得越来越难以理解。

详情

我已经创建了自己的 Bundle。我们称它为 HappyBundle。我已经把这个包放在我公司的文件夹里了。所以我很自然地得到了 CompanyHappyBundle

我想专门为这个包制作一个配置文件,因为我希望它可以重复使用。

在我测试时,我创建了以下内容:

# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
import:
path: /tmp

现在,我想要的是能够在我的 Controller 中使用这个值。我只是不知道怎么办。它抛出以下错误:

[Symfony\Component\Config\Exception\FileLoaderLoadException]
There is no extension able to load the configuration for "company_happy" (in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml).

Looked for namespace "company_happy", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution" in /home/user/symfony/src/Company/HappyBundle/Resources/config/config.yml (which is being imported from "/home/user/symfony/app/config/config.yml").

更新

在 config.yml 中,我添加了以下内容:

#app/config/config.yml
imports:
- { resource: "@CompanyHappyBundle/Resources/config/config.yml" }

我还制作了一个配置类,因为我在某处读到这是必需的。我真的认为只制作一个配置文件需要做很多工作。

namespace Company\HappyBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('company_happy');

$rootNode
->children()
->arrayNode('import')
->children()
->scalarNode('attachments_path')->defaultValue('/tmp')->end()
->scalarNode('method')->defaultValue('ALL')->end()
->booleanNode('move_mail')->defaultValue(true)->end()
->booleanNode('mark_read')->defaultValue(true)->end()
->end()
->end()
;

return $treeBuilder;
}
}

我真正要寻找的是实现此功能所需的步骤和要求。 symfony 的特点是它有一百万种方法可以做到这一点。该文档不仅提供了工作流程。

有人可以帮帮我吗?

最佳答案

我已经解决了我自己的问题,但并非没有麻烦。我对 Symfony 的配置系统一点也不满意。

第一步 - 创建配置文件

创建一个名为 config.yml 的文件在 src/<bundle name>/Resources/config/

yourbundle:
param_one: value_one
param_two: value_two
param_three: value_three
param_four: value_four
param_five:
subparam_one: subvalue_one
subparam_two: subvalue_two
subparam_three: subvalue_three
subparam_four: subvalue_four

第二步 - 导入配置文件

转到 app/config/config.yml并添加:

#app/config/config.yml
imports:
- { resource: "@YourBundle/Resources/config/config.yml" }

第三步 - 创建配置类

创建一个名为 Configuration.php 的文件在 src/<bundle name>/DependencyInjection/

namespace YourBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('yourbundle');

$rootNode
->children()
->scalarNode('param_one')->defaultValue('value_one')->end()
->scalarNode('param_two')->defaultValue('value_two')->end()
->scalarNode('param_three')->defaultValue('value_three')->end()
->scalarNode('param_four')->defaultValue('value_four')->end()
->arrayNode('param_five')
->children()
->scalarNode('subparam_one')->defaultValue('subvalue_one')->end()
->scalarNode('subparam_two')->defaultValue('subvalue_two')->end()
->scalarNode('subparam_three')->defaultValue('subvalue_three')->end()
->scalarNode('subparam_four')->defaultValue('subvalue_four')->end()
->end()
->end()
;

return $treeBuilder;
}
}

第四步 - 创建扩展

最后但同样重要的是,您必须创建一个扩展。创建文件 <yourbundle>Extension.phpsrc/<your bundle>/DependencyInjection/

namespace YourBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class YourbundleExtension extends Extension
{
/**
* @var ContainerBuilder
*/
protected $container;

/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->container = $container;

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

foreach ($config as $key => $value) {
$this->parseNode('yourbundle.'.$key, $value);
}

$container->setParameter('yourbundle', $config);
}

/**
* @param string $name
* @param mixed $value
*
* @throws \Exception
*/
protected function parseNode($name, $value)
{
if (is_string($value)) {
$this->set($name, $value);

return;
}
if (is_integer($value)) {
$this->set($name, $value);

return;
}
if (is_array($value)) {
foreach ($value as $newKey => $newValue) {
$this->parseNode($name.'.'.$newKey, $newValue);
}

return;
}
if (is_bool($value)) {
$this->set($name, $value);

return;
}
throw new \Exception(gettype($value).' not supported');
}

/**
* @param string $key
* @param mixed $value
*/
protected function set($key, $value)
{
$this->container->setParameter($key, $value);
}
}

所有这些步骤都是必需的,只是为了能够调用特定于您的包的配置参数。

如果你们中的任何人知道有什么方法可以更轻松地做到这一点,请随时发表答案或发表评论。

关于php - 在 Symfony 中创建配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32482622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com