如何在其他配置文件中获取配置文件值-Laravel 5


How get config file values in other config file - Laravel 5

我有两个配置文件,"config/oauth-5-laravel.php"answers"config/wtv.php

"oauth-5-laravel.php":

'consumers' => [
    'Facebook' => [
        'client_id'     => config('wtv.social.facebook.client_id'),
        'client_secret' => config('wtv.social.facebook.client_secret'),
        'scope'         => ['email', 'public_profile', 'user_location', 'user_hometown', 'user_birthday', 'user_about_me'],
    ],
    'Google' => [
        'client_id'     => config('wtv.social.google.client_id'),
        'client_secret' => config('wtv.social.google.client_secret'),
        'scope'         => ['profile', 'email'],
    ],
    'Twitter' => [
        'client_id'     => config('wtv.social.twitter.client_id'),
        'client_secret' => config('wtv.social.twitter.client_secret'),
    ],
]

"wtv.php":

'social' => [
    'facebook' => [
        'client_id' => '1696561373912147',
        'client_secret' => '496dbca22495c95ddb699c2a1f0397cf',
    ],
    'google' => [
        'client_id' => '647043320420-tted6rbtl78iodemmt2o2nlglbu7gvsg.apps.googleusercontent.com',
        'client_secret' => 'PLpQ5lv5lT_wV9ElXzAKfrOD',
    ],
    'twitter' => [
        'client_id' => 'dQYHIZDQZGDSLZy4ZDto9lxBh',
        'client_secret' => 'je1aFIFbkH4UpqPDZgEqoCIAg1Ul6lO67JoycDAzS2EzCZqszk',
    ],
],

而不是工作。

坏消息:您不能在配置文件中使用config(),因为一个配置文件无法确保其他配置文件已加载。

好消息:有正确的方法来执行任务。

首先,在wtv.php中有一个数组密钥,称为client_secret。您可以看到,秘密。所有机密数据不应在配置文件和VCS存储库(例如Git存储库)中。

相反,应该使用不在repo中的.env文件。

您的案例将如下所示:

.env

facebook_client_id=1696561373912147
facebook_client_secret=496dbca22495c95ddb699c2a1f0397cf
...

oauth-5-laravel.php

"消费者"=>[

'Facebook' => [
    'client_id'     => env('facebook_client_id'),
    'client_secret' => env('facebook_client_secret'),
...

wtv.php

"社交"=>[

'facebook' => [
    'client_id' => env('facebook_client_id'),
    'client_secret' => env('facebook_client_secret'),
...

通过更改.env文件中的变量,您还可以使用不同的数据来测试(dev)和运行(production)服务器。

.env文件不是php文件,而是由php dotenv解析的常规文本文件。

阅读有关环境配置的更多信息:http://laravel.com/docs/5.1#environment-配置