lumen 5.6 设置APP_KEY为32位长的随机字符串

在 App\Console\Commands下 添加以下内容的KeyGenerateCommand.php文件

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class KeyGenerateCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'key:generate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Set the application key';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $key = $this->generateRandomKey();

        file_put_contents(base_path('.env'), preg_replace(
            '/^APP_KEY=[\w]*/m',
            'APP_KEY='.$key,
            file_get_contents(base_path('.env'))
        ));

        $this->info("Application key [$key] set successfully.");
    }

    /**
     * Generate a random key for the application.
     *
     * @return string
     */
    protected function generateRandomKey()
    {
        return str_random(32);
    }

}

将指令注入
修改App\Console 下的Kernel.php 文件

 protected $commands = [
        //注入指令
        'App\Console\Commands\KeyGenerateCommand', 
    ];

现在可以使用 php artisan key:generate 指令 修改 .env中的APP_KEY 的值

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Laravel 学习交流 QQ 群:375462817 本文档前言Laravel 文档写的很好,只是新手看起来会有...
    Leonzai阅读 12,604评论 2 12
  • 先说几句废话,调和气氛。事情的起由来自客户需求频繁变更,伟大的师傅决定横刀立马的改革使用新的框架(created ...
    wsdadan阅读 8,242评论 0 12
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,477评论 19 139
  • 根目录 App目录 app 目录包含应用程序的核心代码。 Bootstrap目录 bootstrap 目录包含了一...
    伊Summer阅读 7,680评论 0 1
  • 孟飞火了!作为火遍中国篮球圈的‘擦地哥’,孟飞写给篮协领导的一封信在网络上不胫而走,信中朴实无华的表达了自己希望在...
    篮球白痴阅读 4,485评论 2 2