如何在服务器上清除Laravel路由缓存

82

关于本地主机的路由缓存问题

关于本地主机

我在我的route.php文件中有两个路由,它们都很好用,没有问题。我正在学习route:clear和route:cache,并发现以下小问题。

如果我在我的route.php文件中注释掉任何一个路由,然后运行以下命令:

php artisan route:cache

现在路由列表已经被缓存,所以这将保持路由禁用状态。现在,请转到route.php文件并尝试删除注释掉的路由,然后尝试运行启用的URL。仍然会显示404错误,因为我需要使用以下命令来清除缓存

php artisan route:clear

到目前为止,本地主机上的一切都是可以理解的。没有问题。

在godaddy共享托管服务器上部署之后

问题: 我该如何在服务器上删除路由缓存?

7个回答

85

如果你想在服务器上清除路由缓存,请删除这个文件:

bootstrap/cache/routes.php

如果你想更新它,只需运行php artisan route:cache命令,并将bootstrap/cache/routes.php上传到服务器即可。


你能说一下为什么我们应该使用路由缓存吗?我的意思是如果我们不使用路由缓存会有什么问题? - Pankaj
3
@Helper 不使用路由缓存也没有问题。正如文档中所述,它只会使您的路由注册速度提高100倍。 - Dees Oomens

50

如果您正在通过GIT从本地计算机上传文件,那么在连接到实时服务器使用BASH或类似工具时,您可以使用与本地计算机上使用的相同命令。您可以像在本地使用一样使用它。

php artisan cache:clear

php artisan route:cache

应该能够正常工作。


39

您的情况解决方案是:

php artisan cache:clear
php artisan route:cache

优化路由加载在生产环境中是必须的:

如果您正在构建具有许多路由的大型应用程序,应确保在部署过程中运行route:cache Artisan命令:

php artisan route:cache

这个命令可以将所有路由注册缩减为一个方法调用并放入缓存文件中,在注册大量路由时可以提高性能。

由于此功能使用了PHP序列化,因此您只能缓存只使用基于控制器的路由的应用程序的路由信息。PHP无法序列化闭包。

Laravel 5清除路由、视图、配置和应用程序的所有缓存数据

我想分享我的经验和解决方案。在使用gitlab开发我的Laravel电子商务网站时,突然遇到了一个问题,我的视图缓存出现了错误。我尝试过很多方法来刷新,但是我的视图没有任何变化,最后我使用了Laravel命令解决了问题,下面是我添加的一些命令以清除视图、路由、配置等缓存。

重新优化类加载器:

php artisan optimize

清除缓存门面值:

php artisan cache:clear

清除路由缓存:

php artisan route:clear

清除 View 缓存:

php artisan view:clear

清除配置缓存:

php artisan config:clear

如果您正在使用 Laravel Sail,您可以在 WSL 提示符中使用以下命令:./vendor/bin/sail artisan cache:clear。 - Paulo Costa

16

你可以在web.php中定义路由。

Route::get('/clear/route', 'ConfigController@clearRoute');

并使ConfigController.php变成如下内容

   class ConfigController extends Controller
{
    public function clearRoute()
    {
        \Artisan::call('route:clear');
    }
}

并且在服务器上转到该路由示例:http://your-domain/clear/route


2
嗯,天才...你能告诉我当我们无法清除路由缓存时如何定义这条路由以清除我们的缓存吗? - Matthew
你可以用以下所有清除命令中的任意一个替换 "route:clear": "cache:clear", "view:clear", "config:cache"。 - Bakhtiar

3

我建议您使用php artisan optimize

optimize执行以下任务:

  • 配置缓存成功清除。
  • 配置缓存成功缓存。
  • 路由缓存成功清除。
  • 路由成功缓存。
  • 文件成功缓存。此外,您可以在一个命令中完成所有操作。

2

如果您想在服务器上清除路由缓存,请打开托管提供商(例如GoDaddy,NameCheap)提供的终端,并运行以下命令:

php artisan cache:clear
php artisan route:clear

如果您找不到终端,则可以创建一条路由以清除服务器上的缓存:

<?php

use Illuminate\Support\Facades\Artisan;

Route::get('/clear-cache', function () {
   Artisan::call('cache:clear');
   Artisan::call('route:clear');

   return "Cache cleared successfully";
});

You can check this example


-1

在共享主机上清除缓存而无需使用Artisan命令

如何从浏览器中清除路由缓存

 Route::get('/route-cache', function() {
     $exitCode = Artisan::call('route:cache');
     return 'Routes cache cleared';
 });

如何从浏览器中清除配置缓存
 Route::get('/config-cache', function() {
     $exitCode = Artisan::call('config:cache');
     return 'Config cache cleared';
 });

如何从浏览器中清除应用程序缓存

 Route::get('/clear-cache', function() {
     $exitCode = Artisan::call('cache:clear');
     return 'Application cache cleared';
 });

如何清除浏览器的视图缓存

 Route::get('/view-clear', function() {
     $exitCode = Artisan::call('view:clear');
     return 'View cache cleared';
 });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接