gpt4 book ai didi

configuration - 如何使用symfony2访问 Controller 中的语义配置?

转载 作者:行者123 更新时间:2023-12-04 13:53:20 24 4
gpt4 key购买 nike

我正在关注symfony 2的“如何公开捆绑软件的语义配置”官方手册。

我有我的configuration.php

namespace w9\UserBundle\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('w9_user');

$rootNode
->children()
->scalarNode('logintext')->defaultValue('Zareejstruj się')->end()
->end()
;

return $treeBuilder;
}
}

和w9UserExtension.php:
namespace w9\UserBundle\DependencyInjection;

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

class w9UserExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}

听起来很傻,但是我找不到方法,如何在 Controller 中访问logintext参数?
$logintext = $this->container->getParameter("w9_user.logintext");

不起作用。

我做错了什么?

最佳答案

w9UserExtension.php行之后的processConfiguration中,只需添加

$container->setParameter('w9_user.logintext', $config['logintext']);

关于configuration - 如何使用symfony2访问 Controller 中的语义配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10163620/

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