Laravel No supported encrypter found. The cipher and / or key length are invalid.
克隆了一个Laravel的项目到本地,运行是出现这个错误:
RuntimeException in EncryptionServiceProvider.php line 29:
No supported encrypter found. The cipher and / or key length are invalid.
这个是因为项目根目录下的 .env 文件中的 APP_KEY 没设置对,默认是 APP_KEY=SomeRandomString,这个要改成自己的,但也不是随便改就行,Laravel 有提供可以通过命令行生成这个 key,在项目根目录下执行:
php artisan key:generate
完成后 .env 文件中的 APP_KEY 被修改成类似:
APP_KEY=4Hdy4BkW5JB0IyYAM9z7eV6HTtoXtX1c
至于这个命令做了什么,可以看这里 \Illuminate\Foundation\Console\KeyGenerateCommand :
$key = $this->getRandomKey($this->laravel['config']['app.cipher']); if ($this->option('show')) { return $this->line('<comment>'.$key.'</comment>'); } $path = base_path('.env'); if (file_exists($path)) { file_put_contents($path, str_replace( 'APP_KEY='.$this->laravel['config']['app.key'], 'APP_KEY='.$key, file_get_contents($path) )); }
其中:
protected function getRandomKey($cipher) { if ($cipher === 'AES-128-CBC') { return Str::random(16); } return Str::random(32); }
就是说这个 key 是根据 config/app.php 中的 cipher 值来决定长度是16还是32的随机字符串,Laravel 中默认是:
'cipher' => 'AES-256-CBC',
也就是32。所以其实可以不通命令行,直接修改 key,但必须是 key 长度必须是 32 就可以了。
前一篇: 安装 yii2 出现 Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist
后一篇: PHP7 下安装 memcache 和 memcached 扩展