adrian7's avatar

Is it possible to use the Config facade from CLI

Does lumen supports using the Config facade from a CLI command? e.g.

Config::get('app.key');
0 likes
8 replies
adrian7's avatar

Wrote this simple test command but does not seem to work:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Symfony\Component\Console\Input\InputArgument;


class ConfigGetCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $name = 'config:get';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Outputs value of a config key';


    /**
     * Register command arguments
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['key', InputArgument::OPTIONAL, 'The config key that should be returned.'],
        ];
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $key  = $this->argument('key') ?: 'app.key';
        $key  = strval($key);

        $this->comment(PHP_EOL . $key . ' = ' . Config::get($key, null) . PHP_EOL);
    }
}

martinbean's avatar

@adrian7 You‘re supposed to reference the façade by the name its bound to in your app/config.php file. For example:

use Config;
martinbean's avatar

@adrian Come to think of it, Lumen doesn’t have a configuration file. Values are loaded via environment variables. So there is no notion of the “Config” façade.

adrian7's avatar

@martinbean well Config::get(.. works in the browser app. was expecting it to work in the console too... .

adrian7's avatar

@martinbean okay so the fact that it works in the browser app is actually an accident!!!? :P

adrian7's avatar
adrian7
OP
Best Answer
Level 1

Dunno why it worked in browser without this, but added the following code to bootstrap/app.php, just before return, and now works like a charm:

/**
 * Load configuration files
 */
foreach ( glob( base_path('config/*.php') ) as $cfg ) {
    $app->configure( basename($cfg, '.php') );
}

Please or to participate in this conversation.