如何在其他配置文件中获取配置文件值 - Laravel 5
How get config file values in other config file - Laravel 5
我有两个配置文件,“config/oauth-5-laravel.php”和“config/wtv.php",我需要将 wtv.php 文件的值放入另一个名为 oauth-5-laravel.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 存储库)中。
相反,.env
应该使用不在 repo 中的文件。
您的案例将如下所示:
.env
facebook_client_id=1696561373912147
facebook_client_secret=496dbca22495c95ddb699c2a1f0397cf
...
oauth-5-laravel.php
'consumers' => [
'Facebook' => [
'client_id' => env('facebook_client_id'),
'client_secret' => env('facebook_client_secret'),
...
wtv.php
'social' => [
'facebook' => [
'client_id' => env('facebook_client_id'),
'client_secret' => env('facebook_client_secret'),
...
您还可以通过更改 .env
文件中的变量,将不同的数据用于测试(开发)和实时(生产)服务器。
.env
文件不是 php 文件,而是将由 PHP dotenv.
解析的常规文本文件
阅读更多关于环境配置的信息:http://laravel.com/docs/5.1#environment-configuration
我有两个配置文件,“config/oauth-5-laravel.php”和“config/wtv.php",我需要将 wtv.php 文件的值放入另一个名为 oauth-5-laravel.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 存储库)中。
相反,.env
应该使用不在 repo 中的文件。
您的案例将如下所示:
.env
facebook_client_id=1696561373912147
facebook_client_secret=496dbca22495c95ddb699c2a1f0397cf
...
oauth-5-laravel.php
'consumers' => [
'Facebook' => [
'client_id' => env('facebook_client_id'),
'client_secret' => env('facebook_client_secret'),
...
wtv.php
'social' => [
'facebook' => [
'client_id' => env('facebook_client_id'),
'client_secret' => env('facebook_client_secret'),
...
您还可以通过更改 .env
文件中的变量,将不同的数据用于测试(开发)和实时(生产)服务器。
.env
文件不是 php 文件,而是将由 PHP dotenv.
阅读更多关于环境配置的信息:http://laravel.com/docs/5.1#environment-configuration