问题描述
当我清除 Laravel 5.2 项目中的缓存时,我看到以下错误消息:
When I clear caches in my Laravel 5.2 project, I see this error message:
[逻辑异常]无法为序列化准备路由 [面板].使用闭包.
[LogicException] Unable to prepare route [panel] for serialization. Uses Closure.
我认为它与路线有关
Route::get('/article/{slug}', 'Front@slug');
与我的控制器中的特定方法相关联:
associated with a particular method in my controller:
public function slug($slug) {
$article = Article::where('slug',$slug)->first();
$id = $article ->id_article ;
if ( ($article=== null) || (is_null($id)) ) return view('errors/Db');
else return view('detail')->with(array('article'=> $article, 'title'=>'My title - '.$article->title));
}`
简而言之,我从主视图传递 $slug,即文章的短链接,使用 $slug(在数据库中是唯一的),识别记录,然后将其内容传递给详细视图.
In short, from a master view I pass $slug, that is a shortlink to the article, with $slug , which is unique in the database, I identify the record and then I pass it's contents to the detail view.
我在编写该方法时没有遇到任何问题,事实上它就像一个魅力,但在我清理缓存后,我收到了那个错误,并且主视图中的链接没有显示任何短代码.
I didn't have any problem when I wrote the method, infact it worked like a charm, but after I cleaned caches, I get that error and the links in the master view don't show any shortcode.
我哪里做错了?
推荐答案
Route::get('/article/{slug}', 'Front@slug');
与我的控制器中的特定方法相关联:
associated with a particular method in my controller:
不,不是这样.错误消息来自 route:cache
命令,不知道为什么清除缓存会自动调用它.
No, thats not it. The error message is coming from the route:cache
command, not sure why clearing the cache calls this automatically.
问题是一个使用闭包而不是控制器的路由,它看起来像这样:
The problem is a route which uses a Closure instead of a controller, which looks something like this:
// Thats the Closure
// v
Route::get('/some/route', function() {
return 'Hello World';
});
由于闭包不能被序列化,当你有使用闭包的路由时,你不能缓存你的路由.
Since Closures can not be serialized, you can not cache your routes when you have routes which use closures.
这篇关于laravel 无法准备路由......以进行序列化.使用闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!