简体   繁体   中英

How can I get variable from the file config.yml?

Welcome I have a file config.yml

d_facebook:
    file:   %kernel.root_dir%/../src/FacebookBundle/Facebook/FacebookInit.php
    app_id: 296925470418713
    secret: fb4d0a99bcf80e55f47077ffadcac921
    cookie: true
    permissions: [email, user_birthday, user_location, user_about_me, user_location]

Then I have this class, which retrieves data from the file config.yml

<?php

namespace D\FacebookBundle\Facebook;

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use D\FacebookBundle\Facebook\Facebook;
use D\FacebookBundle\Facebook\FacebookConfig;

class FacebookInit
{
    public $hAPI;
    public $userProfile;
    public $user;
    public $logoutUrl;
    public $loginUrl;
    public $config;

    public function __construct()
    {
        $this->config = Yaml::parse('/facebookx/app/config/config.yml');

        $this->hAPI = new Facebook(array(
            'appId' => $this->config['d_facebook']['app_id'],
            'secret' => $this->config['d_facebook']['secret']
            ));
        $this->setUserProfile();
    }
}

What better way to do this? How can I get variable from the file config.yml? I ask for an example?

In your config file you would have something like:

parameters:
    d_facebook:
        file:   %kernel.root_dir%/../src/FacebookBundle/Facebook/FacebookInit.php
        app_id: 296925470418713
        secret: fb4d0a99bcf80e55f47077ffadcac921
        cookie: true
        permissions: [email, user_birthday, user_location, user_about_me, user_location]

From a S2 controller you can get an array of the parameters with:

$faceBookParameters = $this->container->getParameter('d_facebook');

However you probably want to make your Facebook class a service and use dependency injection to set the parameters. So in your services.yml:

services:        
    facebook:
        class:  Path\To\Facebook
        arguments:  
            - '%d_facebook%'  # This injects your parameters

The in your controller you would just do:

$facebook = $this->get('facebook');

Read through the section on services again. http://symfony.com/doc/current/book/service_container.html

Perhaps you want to do something like expose a semantic configuration for a bundle ?

This is if you want to get the configuration from a root level configuration 'namespace' (like 'd_facebook'). You would create a Configuration and an Extension class in your bundle's DependencyInjection folder. The configuration class would have a 'getConfigTreeBuilder' method that would define settings for the keys that can be in your namespace. Then the extension class would have a 'load' method that would receive the parsed configuration and be able to use $container->setParameter('key', 'value') to allow access of the configuration in other classes. The linked cookbook has further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM