Lumen 微框架 => php artisan key:generate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30344141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:43:53  来源:igfitidea点击:

Lumen Micro Framework => php artisan key:generate

phplumen

提问by Thomas Venturini

I'm trying out the PHP micro Framework Lumen (from Laravel).

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

One of my first steps was to look into the .env.examplefile and make a copy of it to have my .envfile. There is a variable APP_KEY just like there is in Laravel. Now I tried out the simple command php artisan key:generateto get my new key But I ran into the following error message:

我的第一步是查看.env.example文件并复制它以保存我的.env文件。有一个变量 APP_KEY 就像在 Laravel 中一样。现在我尝试了简单的命令php artisan key:generate来获取我的新密钥但是我遇到了以下错误消息:

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

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

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

有人知道如何为 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.

所以我找到了我最喜欢的这个问题的解决方案。在命令行 (Linux) 上,我运行php -r "echo md5(uniqid()).\"\n\";"什么给我这样的东西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 ~/.bashrcor vi ~/.bashrcand copy the following alias at the end of the file, alias phpkey='php -r "echo md5(uniqid()).\"\n\";"'. Now you can use the command phpkeywhich will give you a 32 character long random string :)

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

回答by lukasgeiter

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:

Laravel 命令相当简单。它只是生成一个随机的 32 个字符长的字符串。你可以在 Lumen 中做同样的事情。只是临时添加一个这样的路由:

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

Then go to /keyin your browser and copy paste the key into your .envfile.
Afterwards remove the route.

然后转到/key浏览器并将密钥复制粘贴到您的.env文件中。
然后删除路由。

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

显然,您也可以在线使用一些随机字符串生成器。像这个

回答by krisanalfa

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

首先,你必须注册你的密钥生成器的命令,把这个流明密钥发生器命令app/Console/Commands/KeyGenerateCommand.php。要使此命令在 中可用artisan,请更改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\Repositoryinstance has app.keyvalue. To do this, change bootstrap/app.php:

之后,配置您的应用程序,以便该Illuminate\Config\Repository实例具有app.key价值。为此,请更改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.examplefile to .env:

之后,将您的.env.example文件复制到.env

cp .env.example .env

Ignore this step if you already use .envfile.

如果您已经使用.env文件,请忽略此步骤。

Enjoy you key:generatecommand via:

key:generate通过以下方式享受您的命令:

php artisan key:generate


Edit

编辑

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

您可以使用流明发生器。它涵盖了您在 Laravel 中缺少的许多命令。

回答by Jeroen Noten

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

一个简单的解决方案是从终端运行 PHP 代码(不使用tinker,因为 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.

它使用 Laravel 的Str::random()功能,该random_bytes()功能利用了安全功能。

回答by Fábio N Lima

For me the easiest way to generate a Lumen key is typing on console one of these commands:

对我来说,生成 Lumen 密钥的最简单方法是在控制台上输入以下命令之一:

date | md5
date | md5sum

or

或者

openssl rand -base64 24

openssl rand -base64 24

depending of your environment. In my case, I aways use date | md5on mac

取决于你的环境。就我而言,我不会date | md5在 mac 上使用

回答by Cosmitar

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_randomcan help us, but this function is part of Laravel/Lunmen framework. I recommend running tinker

APP_KEY 生成是开发过程的一个步骤(我不认为创建临时路由是一种实用的方法)。该函数str_random可以帮助我们,但该函数是 Laravel/Lunmen 框架的一部分。我建议运行修补程序

php artisan tinker

php artisan tinker

and then run the function

然后运行函数

>>> str_random(32)

>>> str_random(32)

The result is the key you're looking for.

结果就是您正在寻找的密钥。

=> "y3DLxnEczGWGN4CKUdk1S5GbMumU2dfH"

=> "y3DLxnEczGWGN4CKUdk1S5GbMumU2dfH"

回答by Chad Kieffer

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

只需使用 PHP CLI。从本地或远程命令行运行此命令以生成随机的 32 个字符的 Lumen APP_KEY:

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

Output: bae48aba23b3e4395b7f1b484dd25192

输出:bae48aba23b3e4395b7f1b484dd25192

Works with PHP 7.x on Mac and Windows.

适用于 Mac 和 Windows 上的 PHP 7.x。

回答by Umang Soni

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

要生成密钥并使用 laravel 命令,您需要安装一个包。详情如下:

  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.phpfile.
  1. 你必须安装包 composer require flipbox/lumen-generator
  2. 您必须添加$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);bootstrap/app.php文件中。

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

链接:https: //github.com/flipboxstudio/lumen-generator

回答by Juan M. Martínez

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

$2y$10$jb3kw/vUANyzZ4ncMa4rwuR09qldQ2OjX8PGrVB5dIlSnUAPCGjFe

回答by ubuntugod

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

我在 mac 上所做的就是在终端中执行这个命令:

date | md5 | pbcopy

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

这会将值复制到剪贴板,因此您可以轻松地将密钥粘贴到.env文件中。

回答by roerjo

Run php -ato start up interactive php playground.

运行php -a以启动交互式 php 游乐场。

Then run echo substr(md5(rand()), 0, 32);to generate a 32 character string.

然后运行echo substr(md5(rand()), 0, 32);生成一个32个字符的字符串。

You can then copy/paste into the .envfile.

然后,您可以复制/粘贴到.env文件中。