Lumen Micro Framework = > php artian key: generate

我正在试用 PHP 微框架 Lumen (来自 Laravel)。

我的第一个步骤之一是查看 .env.example文件,并制作一个它的副本,以便拥有我的 .env文件。有一个变量 APP _ KEY,就像在 Laravel 一样。现在我尝试使用简单的命令 php artisan key:generate来获取新的密钥,但是我遇到了以下错误消息:

[ InvalidArgumentException ] 在“ key”中没有定义任何命令 命名空间。

有人知道我怎么为 Lumen 生成密钥吗?

更新解决方案

所以我找到了我最喜欢的解决这个问题的方法。在命令行(Linux)上,我运行 php -r "echo md5(uniqid()).\"\n\";",它给我类似于这样的 7142720170cef01171fd4af26ef17c93

如果要更频繁地使用 Lumen,可能需要在主目录 /home/USERNAME中的 .bashrc中创建别名。为此,可以使用 nano ~/.bashrcvi ~/.bashrc打开该文件,并在文件末尾复制以下别名 alias phpkey='php -r "echo md5(uniqid()).\"\n\";"'。现在您可以使用命令 phpkey,它将为您提供一个32个字符长的随机字符串:)

94466 次浏览

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.

The APP_KEY generation is a step of development process (I don't think that creating temporarily routes is a practical way to do it). The function str_random can help us, but this function is part of Laravel/Lunmen framework. I recommend running tinker

php artisan tinker

and then run the function

>>> str_random(32)

The result is the key you're looking for.

=> "y3DLxnEczGWGN4CKUdk1S5GbMumU2dfH"

This answer was inspired by @thomas-venturini 's update to the question. Here's a bash script that takes care of creating .env and updating it with an APP_KEY using the aforementioned PHP command and the UNIX sed command:

#!/usr/bin/env bash


function generate_app_key {
php -r "echo md5(uniqid()).\"\n\";"
}


APP_KEY=$(generate_app_key)


sed -e s/APP_KEY=.*$/APP_KEY=${APP_KEY}/g .env.example > .env

Hope someone finds this useful.

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.

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

I have used these commands:

php -r \"copy('.env.example', '.env');\"


php -r "echo password_hash(uniqid(), PASSWORD_BCRYPT).\"\n\";"

The command generates a key similar to this:

$2y$10$jb3kw/vUANyzZ4ncMa4rwuR09qldQ2OjX8PGrVB5dIlSnUAPCGjFe

All I do on mac is execute this command in the terminal:

date | md5 | pbcopy

This copies the value into the clipboard and so you can easily paste the key into the .env file.

To generate key and use laravel command you need to install one package. The details are as below:

  1. You have to install package composer require flipbox/lumen-generator
  2. You have to add $app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class); into bootstrap/app.php file.

Link: https://github.com/flipboxstudio/lumen-generator

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.

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 🐘 v7.3.0
➜ lumen-key
VkxTYWZZSnhVNVEzRThXelBGZVJDVGZVYTNTcm9peHY=

You can also remove the third step by adding the key directly in .env using PHP.

Simply use PHP CLI. Run this from your local or a remote command line to generate a random 32-character Lumen APP_KEY:

php -r "echo bin2hex(random_bytes(16));"

Output: bae48aba23b3e4395b7f1b484dd25192

Works with PHP 7.x on Mac and Windows.

[Flipbox\LumenGenerator]

Fix error: there are no comands defined...

[boostrap/app] Check if you register the Flipbox\LumenGenerator after return $app. If so move the Service provider register before return app...

/**
* Configure extra LARAVEL commands to a lumen app
* Check avaliable commands in git: flipboxstudio lumen-generator
*/
if($app->environment() !== 'production'){
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
}


return $app;


Lumen 8.0 / flipbox/lumen-generator 8.2

It Worked 100%

Simply install the flipbox/lumen-generator package

composer require flipbox/lumen-generator.

Inside your bootstrap/app.php file, add:

$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);

Then after you can able to run php artisan commands, more info: https://github.com/flipboxstudio/lumen-generator