Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen Micro Framework => php artisan key:generate

Tags:

php

lumen

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 .env file. There is a variable APP_KEY just like there is in Laravel. Now I tried out the simple command php artisan key:generate to get my new key But I ran into the following error message:

[InvalidArgumentException] There are no commands defined in the "key" namespace.

Does some one know how I can generate keys for Lumen?

Update with solution

So I found my favorite solution for this problem. On the command line (Linux) I run php -r "echo md5(uniqid()).\"\n\";" what gives me something like this 7142720170cef01171fd4af26ef17c93.

If you are going to use Lumen more often, you may want to create an alias in your .bashrc, which is located in your home directory /home/USERNAME. To do so, you can open the file with nano ~/.bashrc or vi ~/.bashrc and copy the following alias at the end of the file, alias phpkey='php -r "echo md5(uniqid()).\"\n\";"'. Now you can use the command phpkey which will give you a 32 character long random string :)

like image 888
Thomas Venturini Avatar asked May 20 '15 08:05

Thomas Venturini


2 Answers

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

like image 172
lukasgeiter Avatar answered Oct 14 '22 10:10

lukasgeiter


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.

like image 31
krisanalfa Avatar answered Oct 14 '22 10:10

krisanalfa