Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get value from config in Lumen

Tags:

laravel

lumen

I want to change the timezone in lumen, but I cannot get the value from config, it always give the default value UTC.

I've tried everything I know, to the point changing the default value to what I wanted. But still the timezone wont change

AppServiceProvider

public function register()
    {
        //set local timezone
        date_default_timezone_set(config('app.timezone'));
        //set local date name
        setLocale(LC_TIME, $this->app->getLocale());
        
        URL::forceRootUrl(Config::get('app.url'));
    }

Bootstrap.app

(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();

date_default_timezone_set(env('APP_TIMEZONE', 'Asia/Jakarta'));

$app->configure('app');

Config.app

'timezone' => env("APP_TIMEZONE", "Asia/Jakarta"),

.env

APP_TIMEZONE="Asia/Jakarta"
APP_LOCALE="id"

Also if I make a variable inside config.app such as:

'tes_var' => 'Test'

And using it like this:

\Log::info(config('app.tes_var'));

The result in Log is null, I can't get the value from tes_var.

I don't have any idea what's wrong here, if it's in Laravel maybe this is happened because cached config, but there's no cached config in Lumen. Maybe I miss some configuration here?

Thanks

like image 398
Catto Avatar asked Dec 17 '25 05:12

Catto


2 Answers

First, you should create the config/ directory in your project root folder. Then create a new file app.php under the config directory i.e. config/app.php

Now add whatever config values you want to access later in your application in the config/app.php file.

So, instead of creating a config.php file you should make a config directory and can create multiple config files under the config directory.

So final code will be like this:

config/app.php will have:

<?PHP
return [
   'test_var' => 'Test'
];

Can access it anywhere like this:

config('app.tes_var');

Although Lumen bootstrap/app.php has already loaded the app.php config file (can check here: https://github.com/laravel/lumen/blob/9.x/bootstrap/app.php) If not loaded in your case, you can add the below line in bootstrap/app.php file:

$app->configure('app');

Hope it will help you.

like image 142
sssurii Avatar answered Dec 19 '25 18:12

sssurii


In order to use the env file while caching the configs, you need to create a env.php inside the config folder. Then, load all env variables and read as "env.VARIABLE_FROM_ENV". Example env.php:

<?php

use Dotenv\Dotenv;

$envVariables = [];

$loaded = Dotenv::createArrayBacked(base_path())->load();

foreach ($loaded as $key => $value) {
    $envVariables[$key] = $value;
}

return $envVariables;

then read in your code:

$value = config('env.VARIABLE_FROM_ENV', 'DEFAULT_VALUE_IF_YOU_WANT');
like image 35
Henrique Troiano Avatar answered Dec 19 '25 19:12

Henrique Troiano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!