Laravel5学习笔记:执行route:cache时报LogicException - 小众知识

Laravel5学习笔记:执行route:cache时报LogicException

2017-11-28 08:16:38 苏内容
  标签: Laravel
阅读:4082

laravel5的路由支持缓存。需要执行以下命令:

php artisan route:cache1

执行完毕后,报出以下错误:

Route cache cleared!
[LogicException]
Unable to prepare route [/] for serialization. Uses Closure.123

这个异常的错误信息,提示的已经非常明确了:大概意思就是说在闭包里边,是不能够进行路由缓存的。那么现在就有两种办法: 
① 想要继续使用闭包,那就只能放弃路由缓存(至少目前我没有其他办法,如果你有,记得告诉我)。 
② 那就是在路由里边,也就是route.php中,不要使用闭包的方式,统统改为控制器。

具体例子:

// 之前,报错的路由Route::get('/', function(){
    return veiw('welcome');
});// 修改之后,能够路由缓存的方式Route::get('/', 'HomeController@index');12345678

现在就搞定啦。再次执行 php artisan route:cache 可以看到成功的信息提示啦:

Route cache cleared!Routes cached successfully!

an into this little issue when trying to use route:cache on Laravel 5.

Route cache cleared!
[LogicException]
Unable to prepare route [posts/laravel's-new-forelse-construct] for serialization. Uses Closure.

The exception is pretty clear, but it's worth noting that closure type routes will not be compatible when trying to use the route:cache command. You have a couple of options: forgo cached routing and the speed improvement or move your routes into a controller. I did the latter.

// Previously
Route::get('posts/laravel's-new-forelse-construct', function()
{
    return Redirect::to('posts/laravels-new-forelse-construct', 301);
});

// Currently
Route::get('posts/laravel's-new-forelse-construct', 'RedirectsController@getPostsLaravelsNewForelseConstruct');

Now that your routing is handled by a controller instead of a closure you'll be able to cache your routes as you want.



Even with Forge there can still be key differences between a local and online environment. The only two things I can think of right now are:

  1. Your Forge environment may have cached your config files. If so you need to run php artisan config:cache to recache them. Otherwise any changes in your config files - like a new provider in app.php say - wont be picked up when you deploy to your server. I had to ssh in and manually delete the cached file off my server in the past to get a stuck deployment working again

  2. A long shot but double check your camel case name is the same in all references to your interface. GarageWarrantyInterface.php vs interface Garagewarrantyinterface { .. I've had various local and server environments interpret upper case differently


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1