How to make custom settings data available globally in Yii 2?

前端 未结 3 1431
萌比男神i
萌比男神i 2020-12-28 08:55

I\'m creating an application which stores some settings in the database and ideally it would be good to load these settings during bootstrapping and make them available via

相关标签:
3条回答
  • 2020-12-28 09:23

    I'm a little late to the party, but there is a much easier way to do this.

    $config = [
        'id' => 'basic',
        'basePath' => dirname(__DIR__),
        'bootstrap' => [
                        'log',
        ],
        //.............
        'params' = [
            'adminEmail' => 'admin@example.com',
            'defaultImage' => '/images/default.jpg',
            'noReplyEmail' => 'noreply@example.com'
        ],
    ];
    

    Now you can simply access those variables with the following syntax

    $adminEmail = \Yii::$app->params['adminEmail'];
    
    0 讨论(0)
  • 2020-12-28 09:24

    Another way to do it is override init() method in the baseController.

    class BaseController extends Controller{...    
    public function init()
        {    
                if(Yii::$app->cache->get('app_config'))
            {
                $config = Yii::$app->cache->get('app_config');
                foreach ($config as $key => $val)
                {
                    Yii::$app->params['settings'][$key] = $val->value;
                }
            }
            else
            {
                $config = Config::find()->all();
                $config = ArrayHelper::regroup_table($config, 'name', true);
                Yii::$app->cache->set('app_config', $config, 600);
    
                foreach ($config as $key => $val)
                {
                    Yii::$app->params['settings'][$key] = $val->value;
                }
            }
    }
    ....}    
    

    It`s up to you. I used to do this method in Yii1 but now I would prefer bootstrap method

    0 讨论(0)
  • 2020-12-28 09:25

    Ok, I found out how to do it.

    Basically you have to implement the bootstrapInterface, an example below for my situation.

    Set the path to your class that implements the interface:

    app/config/web.php:

    $config = [
        'id' => 'basic',
        'basePath' => dirname(__DIR__),
        'bootstrap' => [
                        'log',
                        'app\base\Settings',
        ],
        //.............
    ];
    

    So I have placed a class called Settings.php at the location: app\base\Settings.php.

    Then this is my Settings.php file:

    namespace app\base;
    
    use Yii;
    use yii\base\BootstrapInterface;
    
    /*
    /* The base class that you use to retrieve the settings from the database
    */
    
    class settings implements BootstrapInterface {
    
        private $db;
    
        public function __construct() {
            $this->db = Yii::$app->db;
        }
    
        /**
        * Bootstrap method to be called during application bootstrap stage.
        * Loads all the settings into the Yii::$app->params array
        * @param Application $app the application currently running
        */
    
        public function bootstrap($app) {
    
            // Get settings from database
            $sql = $this->db->createCommand("SELECT setting_name,setting_value FROM settings");
            $settings = $sql->queryAll();
    
            // Now let's load the settings into the global params array
    
            foreach ($settings as $key => $val) {
                Yii::$app->params['settings'][$val['setting_name']] = $val['setting_value'];
            }
    
        }
    
    }
    

    I can now access my settings via Yii:$app->params['settings'] globally.

    Extra information on other ways to bootstrap stuff here.

    0 讨论(0)
提交回复
热议问题