Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marcelgsantos's avatar

Can't Get Config Data in Lumen

Hello!

I'm not capable get data from config files in Lumen. What I'm doing wrong?

I'm trying to get some config data like 'app' or 'database', for example, from routes closure and controllers and nothing.

I tried to use config helper, follow documentation (http://lumen.laravel.com/docs/configuration#configuration-files) without success...

I would to use custom configuration as seen in Lumen documentation.

Thanks,

Marcel dos Santos

0 likes
10 replies
bretterer's avatar

You need to make sure you are enabling the DOTENV from bootstrap/app.php file. Also, if you are using the facade, you have to enable those as well. You do this in the same file around line 22. Just uncomment $app->withFacades();

marcelgsantos's avatar

Hey @bretterer, thanks for reply!

I had already enabled both too. These was the first things that I did.

Marcel

bretterer's avatar

Here is how I make this work.

Create a directory in the root of your application called config. Within the config directory, you want to create a file just like you would in Laravel. For my example, I used the file name test.php which looked like this:

<?php

return [
    'key' => '12345'
];

Then, in my home route file (you would move this to a more global position (bootstrap/app.php maybe)) You add $app->configure('test') to enable that config file. From within your route or where you need the config you then make sure you are using the facade and run Config::get('test.key'); and that should give you access to it. I have noticed, you need to run $app->configure() for all files, including the default ones you want to override, unlike what the documentation says.

A NOTE: in Lumen, everything is meant to be done via the .env file and you can access these by using the env() function or the PHP $_ENV Global. I would say this is the recommended way as if you look into the vendor/larave/lumen-framework/config/* files, they all reference the environment variables.

Hope this helps!

7 likes
marcelgsantos's avatar

@bretterer, thanks!

I forgot to run $app->configure() and, for this reason, configuration files weren't working. Now, I have other question, where should I put $app->configure()? Can I run it from base Controller?

Thanks,

Marcel dos Santos

1 like
bretterer's avatar

I would do it in your bootstrap/app.php file

1 like
benjam_es's avatar

I placed mine within the AppServiceProvider register method. Make sure you uncomment the following line in bootstrap/app.php:

$app->register(App\Providers\AppServiceProvider::class);

1 like
oh4real's avatar

Took me a bit to sort this out as well.

The confusion comes from the docs not making it clear that a config dir in your project root will be scanned for files matching those in the lumen-framework:

/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/app.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/auth.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/broadcasting.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/cache.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/database.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/filesystems.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/mail.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/queue.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/session.php
/PATH/TO/PROJECT/vendor/laravel/lumen-framework/config/view.php

In one project I added a /PATH/TO/PROJECT/config/queue.php and it was picked up without incident, but in another project, I added /path/to/project/config/services.php to hold microservice host configs, but it wasn't picked up until I added $app->configure('services') to my /bootstrap/app.php

Hope this helps clarify for others in the future.

2 likes
beerbuddha's avatar

For those who are still confused and require a bit more visuals - @oh4real solution was on the right path.

  1. Create a directory on the root level of your project a folder config (just like laravel 5.2) You can include all the basic ones (as showned by oh4real)
...
config/app.php
config/database.php
config/cache.php
...

Then within bootstrap/app.php file:

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});
//...
$app->configure('app');
$app->configure('database');
$app->configure('cache');
//..

return $app;

Then anywhere in your app, you would invoke the respective config via:

config('app');
config('app.key');
//etc

TLDR;

Alternatively, you can make it cleaner by looping through the directory or in my case:

$config = [
    'app',
    'database',
    'cache'
];
$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});

foreach ($config as $file) {
    $app->configure($file);
}

return $app;

will yield the same result

bewhy's avatar

Having the same issue... cant not get any config... now in Auth...

$app->configure('app'); $app->configure('auth');

config/auth.php

Please or to participate in this conversation.