phplaraveloauthconfig

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


I have two config files, "config/oauth-5-laravel.php" and "config/wtv.php", i need to get the values of wtv.php file into another file called oauth-5-laravel.php like this:

"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',
    ],

],

and not work.


Solution

  • Bad news: you can't use config() in config files as one config file can't be sure that other config file was loaded.

    Good news: there's the right way to perform your task.

    First of all, in wtv.php there's array key which is called client_secret. You see, secret. All secret data should not be in config files and VCS repository (e.g. Git repository).

    Instead, .env file which is not in repo should be used.

    Your case will look like this:

    .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'),
        ...
    

    You will also be able to use different data for testing (dev) and live (production) servers by changing variables in .env file.

    .env file is not php file, but regular text file which will be parsed by PHP dotenv.

    Read more about environment configuration: http://laravel.com/docs/5.1#environment-configuration