1. 程式人生 > >lumen 5.6 設置APP_KEY為32位長的隨機字符串

lumen 5.6 設置APP_KEY為32位長的隨機字符串

reg highlight str clas hand for sign nature get

在 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位長的隨機字符串