Lumen Micro Framework => php artisan key:generate

The Laravel command is fairly simple. It just generates a random 32 character long string. You can do the same in Lumen. Just temporarily add a route like this:

$router->get('/key', function() {
    return \Illuminate\Support\Str::random(32);
});

Then go to /key in your browser and copy paste the key into your .env file.
Afterwards remove the route.

Obviously you could also use some random string generator online. Like this one


Firstly, you have to register your key generator command, put this Lumen Key Generator Commands to app/Console/Commands/KeyGenerateCommand.php. To make this command available in artisan, change app\Console\Kernel.php:

/**
 * The Artisan commands provided by your application.
 *
 * @var array
 */
protected $commands = [
    'App\Console\Commands\KeyGenerateCommand',
];

After that, configure your application so that Illuminate\Config\Repository instance has app.key value. To do this, change bootstrap/app.php:

<?php

require_once __DIR__.'/../vendor/autoload.php';

Dotenv::load(__DIR__.'/../');

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

$app->configure('app');

After that, copy your .env.example file to .env:

cp .env.example .env

Ignore this step if you already use .env file.

Enjoy you key:generate command via:

php artisan key:generate

Edit

You may use Lumen Generator. It covers so much commands you are missing from Laravel.


An easy solution is just running PHP code from the terminal (without using tinker, because that is not available with Lumen):

php -r "require 'vendor/autoload.php'; echo str_random(32).PHP_EOL;"

It uses Laravel's Str::random() function that makes use of the secure random_bytes() function.

Tags:

Php

Lumen