首页 > 移动平台 > 详细

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

时间:2019-04-01 22:37:57      阅读:388      评论:0      收藏:0      [点我收藏+]

 

在 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‘,
    ];

 

复制.env.example 为.env文件 

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

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

原文:https://www.cnblogs.com/php-linux/p/10639211.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!