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();
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
Hey @bretterer, thanks for reply!
I had already enabled both too. These was the first things that I did.
Marcel
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
.envfile and you can access these by using theenv()function or the PHP$_ENVGlobal. I would say this is the recommended way as if you look into thevendor/larave/lumen-framework/config/*files, they all reference the environment variables.
Hope this helps!
@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
I would do it in your bootstrap/app.php file
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);
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.
For those who are still confused and require a bit more visuals - @oh4real solution was on the right path.
- 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
The Socialite Providers packages support Lumen out of the box. Full disclosure: they are a collection of packages that I help maintain.
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.