You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9服务器缓存清除失败求助:两种方案均无效

Laravel 9 清除缓存及404问题解决

一、404问题的核心原因与解决

  • 路由未加载:确认你的清除路由写在routes/web.php中,而非api.php或其他路由文件。
  • 路由缓存冲突:若之前执行过route:cache,新添加的路由不会生效。必须先通过命令行执行:
php artisan route:clear

执行后新路由才能被识别,访问你的域名/clear即可正常触发缓存清除。

  • URL路径错误:检查访问地址是否正确,无多余斜杠或自定义路径前缀干扰。

二、推荐的缓存清除方式(命令行更可靠)

web路由清除缓存可能受权限、路由缓存限制,优先用服务器命令行操作,覆盖所有缓存类型:

# 清除应用缓存
php artisan cache:clear
# 清除配置缓存
php artisan config:clear
# 清除路由缓存
php artisan route:clear
# 清除视图缓存
php artisan view:clear
# (可选)清除编译类文件
php artisan clear-compiled

生产环境可按需重新生成缓存以提升性能:

php artisan config:cache
php artisan route:cache

三、web路由清除缓存的修正方案

若坚持使用web方式,先确保执行了route:clear,再修正代码:

方案一修正(补全类引入)

use Illuminate\Support\Facades\Artisan;

Route::get('/clear', function() {
    Artisan::call('cache:clear');
    Artisan::call('config:clear');
    Artisan::call('route:clear');
    Artisan::call('view:clear');
    return "缓存已清除!";
});

方案二修正(补全控制器引入)

路由文件顶部添加控制器引用(根据实际命名空间调整):

use App\Http\Controllers\SiteController;

Route::get('clear',[SiteController::class,'clear']);

控制器方法保持原样即可。

四、网站更新不显示的额外排查点

  • 文件权限:确保storage和bootstrap/cache目录具备服务器运行用户(如www-data)的读写权限。
  • OPcache缓存:若PHP开启OPcache,需重启PHP服务或清除OPcache缓存。
  • 前端静态资源:若为前端更新,可强制刷新浏览器缓存(Ctrl+F5)或给静态资源添加版本号避免浏览器缓存。

内容的提问来源于stack exchange,提问作者palika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 12:25:41