在另一台机器上拉取最新代码后,进入网站后台,发现页面空白,使用 php artisan cache:clear, php artisan clear-compiled 等清除缓存和编译文件后还是不行,打开 PHP 错误日志查看,发现报错:
Fatal error: Declaration of Illuminate\\Auth\\SessionGuard::basic() must be compatible with that of Illuminate\\Contracts\\Auth\\SupportsBasicAuth::basic() in /usr/share/nginx/html/tanteng.me/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 17
在网上找到一个可行的解决办法,修改 composer.json 文件:
ps://oddyzfr8z.qnssl.com/wp-content/uploads/2016/09/laravel-composer-json-post-install.png" />
将 clear-compiled 替换,具体修改如下:
PHP
"post-install-cmd": [
- "php artisan clear-compiled",
+ "Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
- "php artisan clear-compiled",
+ "Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
再次执行 sudo composer update 之后,再执行一次 sudo composer dump-autoload 就恢复正常了。
顺便我们来看看这两个方法,文件路径 laravel/framework/src/Illuminate/Foundation/ComposerScripts.php:
class ComposerScripts
{
/**
* Handle the post-install Composer event.
*
* @param \Composer\Script\Event $event
* @return void
*/
public static function postInstall(Event $event)
{
require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
static::clearCompiled();
}
/**
* Handle the post-update Composer event.
*
* @param \Composer\Script\Event $event
* @return void
*/
public static function postUpdate(Event $event)
{
require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
static::clearCompiled();
}
/**
* Clear the cached Laravel bootstrapping files.
*
* @return void
*/
protected static function clearCompiled()
{
$laravel = new Application(getcwd());
if (file_exists($compiledPath = $laravel->getCachedCompilePath())) {
@unlink($compiledPath);
}
if (file_exists($servicesPath = $laravel->getCachedServicesPath())) {
@unlink($servicesPath);
}
}
}
这两个方法都是清除 Laravel 的编译文件,以后可以使用这两个方法作为 composer.json 中安装和更新依赖的脚本命令。