I\'m trying out the PHP micro Framework Lumen (from Laravel).
One of my first steps was to look into the .env.example
file and make a copy of it to have my
1.Open your terminal setup file:
vim ~/.zshrc
2.Create an alias for generating random strings:
# Lumen Key Generate
alias lumen-key="php -r \"require 'vendor/autoload.php'; echo base64_encode(str_random(32)).PHP_EOL;\""
3.Get a key whenever you need:
~/your-lumen-project via
For me the easiest way to generate a Lumen key is typing on console one of these commands:
date | md5
date | md5sum
or
openssl rand -base64 24
depending of your environment. In my case, I aways use date | md5
on mac
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
Run php -a
to start up interactive php playground.
Then run echo substr(md5(rand()), 0, 32);
to generate a 32 character string.
You can then copy/paste into the .env
file.
To generate key and use laravel command you need to install one package. The details are as below:
composer require flipbox/lumen-generator
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
into bootstrap/app.php
file.Link: https://github.com/flipboxstudio/lumen-generator
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
You may use Lumen Generator. It covers so much commands you are missing from Laravel.