1. 程式人生 > >Laravel No supported encrypter found. The cipher and / or key length are invalid

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

剛部署好Laravel 5.1,開啟首頁的時候提示錯誤,開啟debug模式以後,就看到具體錯誤資訊:No supported encrypter found. The cipher and / or key length are invalid.

問題原因是config/app.php中有一個關於祕鑰型別的配置,預設設定為AES-256-CBC,也就是對應的祕鑰必須是32個字元,而預設配置中寫的是SomeRandomString,只有16個字元。當然我們可以通過將cipher設定為AES-128-CBC來解決這個問題。

但是,畢竟SomeRandomString也不是個像樣的祕鑰,我們可以生成一個。只需要下面這個程式碼:

php artisan key:generate
Application key [tFnLJ1Wo4647KlYxvCsjfMfI2VK5u2Rb] set successfully.

這個時候我們將生成的祕鑰,修改到config/app.php的key欄位中,替換掉SomeRandomString就好了。

更新:其實如果使用 laravel new app name 的方式建立專案,是不會遇到這個問題的,因為那種方式會生成一個配置檔案,這個配置檔案也有關於appkey的設定,具體需要額外執行下面這條命令:

php -r "copy('.env.example', '.env'
)
;" php artisan clear-compiled php artisan optimize php artisan key:generate