相关疑难解决方法(0)

在Symfony中创建配置

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

细节

我创建了自己的Bundle.让我们来称呼它HappyBundle.我把这个Bundle放在我公司的文件夹里.所以我很自然CompanyHappyBundle.

我想专门为这个bundle创建一个配置文件,因为我希望它可以重用.

当我测试我创建了以下内容:

# src/Company/HappyBundle/Resources/config/config.yml
company_happy:
    import:
        path: /tmp
Run Code Online (Sandbox Code Playgroud)

现在,我想要的是能够在我的控制器中使用此值.我只是不知道如何.它抛出以下错误:

[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").
Run Code Online (Sandbox Code Playgroud)

更新

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

#app/config/config.yml
imports:
    - { resource: "@CompanyHappyBundle/Resources/config/config.yml" }
Run Code Online (Sandbox Code Playgroud)

我也做了一个Configuration类,因为我读到了这个必需的地方.我真的认为这只是一个配置文件的工作.

namespace Company\HappyBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public …
Run Code Online (Sandbox Code Playgroud)

php symfony

5
推荐指数
1
解决办法
1211
查看次数

Symfony2 - bundle中的doctrine连接配置

我有一个使用我的附加包的项目.此捆绑包连接到其他数据库,我需要配置另一个数据库.

我希望在2个配置文件中建立此连接.

主配置:

# ROOT/app/config/config.yml:
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   "%database_driver%"
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
Run Code Online (Sandbox Code Playgroud)

捆绑配置:

# src/SecondBundle/Resources/config/config.yml
doctrine:
    dbal:
        connections:
            secondBundle:
                driver:   "%secondBundle.database_driver%"
                host:     "%secondBundle.database_host%"
                port:     "%secondBundle.database_port%"
                dbname:   "%secondBundle.database_name%"
                user:     "%secondBundle.database_user%"
                password: "%secondBundle.database_password%"
                charset:  UTF8
Run Code Online (Sandbox Code Playgroud)

捆绑扩展文件:

class SecondBundleExtension 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('config.yml');
    }
} …
Run Code Online (Sandbox Code Playgroud)

php doctrine symfony

5
推荐指数
2
解决办法
1016
查看次数

标签 统计

php ×2

symfony ×2

doctrine ×1