什么时候在 Laravel 中生成新的应用程序密钥?

2021-12-18 00:00:00 console php laravel laravel-5 laravel-5.1

因为当我创建应用程序时它会自动在我的 .env 文件中为我设置它,所以我不确定我应该什么时候运行它.

Since it automatically sets it for me in my .env file when I create the app, I'm not sure when I should run it.

除此之外,如果第二个开发人员进来并克隆应用程序,他/她是否需要运行 php artisan key:generate ?

In addition to that, if a second developer comes in, and clones the app, does he/she need to run php artisan key:generate ?

我们怎么知道什么时候运行 php artisan key:generate ?

How do we know exactly when to run php artisan key:generate ?

推荐答案

php artisan key:generate 是一个在你的 中设置 APP_KEY 值的命令.env 文件.默认情况下,此命令在 composer create-project laravel/laravel 命令之后运行.如果你使用像 git 这样的版本控制系统来管理你的项目进行开发,调用 git push ... 会将你的 Laravel 项目的副本推送到它要去的任何地方,但不会包含您的 .env 文件.因此,如果有人使用 git clone ... 克隆您的项目,他们将不得不手动输入 php artisan key:generate 以使他们的应用程序正常运行.

php artisan key:generate is a command that sets the APP_KEY value in your .env file. By default, this command is run following a composer create-project laravel/laravel command. If you use a version control system like git to manage your project for development, calling git push ... will push a copy of your Laravel project to wherever it is going, but will not include your .env file. Therefore, if someone clones your project using git clone ... they will have to manually enter php artisan key:generate for their app to function correctly.

因此,TL:DR 唯一一次您需要调用php artisan key:generate 是跟随一个预先创建的cloneLaravel 项目.

So, TL:DR the only time you need to call php artisan key:generate is following a clone of a pre-created Laravel project.

旁注:如果您尝试在 APP_KEY 设置为 SomeRandomString(这是您的 .env.example 中的默认值)的情况下运行 Laravel 项目code> 文件,你实际上会得到一个错误:

Side note: If you try to run a Laravel project with your APP_KEY set to SomeRandomString (which is the default in your .env.example file, you will actually get an error:

未找到受支持的加密器.密码和/或密钥长度无效.

No supported encrypter found. The cipher and / or key length are invalid.

相关文章