ludwigmair's avatar

Ho to access Config variables in laravel 5?

Hi,

i am just wondering how to access Config Variables.

in Laravel 4 it was

 Config::get('filename.varname');

but this does not work in laravel 5

is there any way to do this in laravel 5?

any idea?

thanks Ludwig

0 likes
15 replies
xingfucoder's avatar

Hi @ludwigmair, you can find an .env.example file in the root of your L5 Project.

Rename to .env and you can put there your config values.

Later you may use as follow:

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

There you use the .env database config params for configure your database with the env() helper function that needs two arguments the first the environment config param to use, and the second is a default value.

Hope it helps you.

4jZW7jVSdS4U6PC's avatar

@codeatbusiness this is off topic, what @ludwigmair means is how to access to the configuration variables inside the application. With Laravel 4.x you can use Config::get('') in Laravel 5 there's a contract for that in Illuminate\Contracts\Config\Repository but I don't know if it has got a Facade or something, I still ignoring Laravel 5 until the stable release.

ludwigmair's avatar

Hi,

Hi guys, thanks for the brief response

you are right ludo237:)

what i found out, if i place the vars in config/app.php, i can call them with

config('app.myVarname');
// or 
Config::get('app.myVarname');

but if i create a additional config file in the config folder, i am not able to access this file values.

does anyone know how to do this?

thanks Ludwig

5 likes
ludwigmair's avatar
ludwigmair
OP
Best Answer
Level 2

Hi,

so i found out how it works

you have to add the config file placed in config/ to the ConfigServiceProvider

    public function register()
    {
        config([
            'config/myfile.php',
        ]);
    }
}

hope this helps

Ludwig

4 likes
sdebacker's avatar

Sorry I wasn’t clear.
I try to access a key from a config file in a subfolder: /config/foo/bar.php
This file contains a baz key.

I tried these code with no luck:

config('foo.bar.baz');
config('foo/bar.baz');
sdebacker's avatar

@toniperic Thanks, it works.
But, what if two packages publish a config file with same name, like :
/config/package1/bar.php
/config/package2/bar.php ?

james2doyle's avatar

In /config/app.php you can add a line like so:

'theme_path' => __DIR__.'/../resources/views/theme',

Then in any model/controller/etc. you can access that value via config('app.theme_path') or Config::get('app.theme_path').

If you want to use Config::get('app.theme_path'), make sure you include the Config facade.

cbj4074's avatar

1.) There's no need to use the Config facade to get configuration values; there is now an equivalent helper-method, config().

2.) In Laravel 5, configurations are no longer "namespaced", which is critical to understand when creating a configuration file (and particularly when merging configuration files).

In Laravel 4, the configuration options for a specific package were nested under a namespaced array, like so:

return [
    'myextension' => [
        'host' => 'localhost',
        'port' => 443,
    ],
];

In Laravel 5, the equivalent is:

return [
    'host' => 'localhost',
    'port' => 443,
];

3.) Rather than store your custom configuration values in /config/app.php, a cleaner approach is to store them in your own configuration file, e.g., /config/myname-mypackage.php. You would then retrieve all values with something like:

$config = config('myname-mypackage');

To retrieve only an individual value, simply use dot-notation:

$config = config('myname-mypackage.host');

It is possible to nest configuration files within subdirectories (within /config/), too, which can be necessary in order to prevent conflicts ( @sdebacker asked about this -- great question ).

If you create your configuration file at, for example, /config/myname/mypackage.php, you would access the values using dots in place of the directory separators:

$config = config('myname.mypackage');

4.) It's simple to merge configuration files using a service provider's register() method:

public function register()
{
    $this->mergeConfigFrom(
        __DIR__.'/../config/config.php', 'myname-mypackage'
    );
}

(note that we omit the .php extension in the second argument)

5.) If you're building your own packages, you can specify any number of configuration files to be published when the new vendor:publish artisan command is executed:

public function boot()
{
    $this->publishes([
        __DIR__.'/../config/config.php' => config_path('myname-mypackage.php'),
    ]);
}

For more information, refer to the relevant doc at http://laravel.com/docs/master/packages#configuration .

6 likes
bendubuisson's avatar

if you chuck them in your .env file then you can retrieve like so env('WHATEVER') That's if you have the following value in your .env WHATEVER=somevalue

2 likes
gildniy's avatar

Config::get('configfile.variable'); This works like charm in the controller of L5.1

manshu's avatar

Im still trying to figure this one out for laravel 5.1 any takers ? Old Tutorials are no help.

ABDELRHMAN's avatar

@manshu

Try This

//ConfigFile.php 

    <?php
        return [
            'foo'=>[
                'key'=>'value',
            ]
        ];
    <?      
    
    //Usage

    $app = $this->app;
    
    $config = $app['config']['configfile.foo'];
    
    $value = $config['key']; 
    
    return $value; //value

Please or to participate in this conversation.