问题描述
当我清除Laravel 5.2项目中的缓存时,会看到以下错误消息:
[LogicException] 无法准备要进行序列化的路线[面板].使用闭包.
我认为这与路线有关
Route::get('/article/{slug}', 'Front@slug');
与控制器中的特定方法相关联:
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在数据库中是唯一的,我确定了记录,然后将其内容传递给了详细视图. /p>
当我编写该方法时,我没有任何问题,但实际上它就像一个魅力一样工作,但是在清除缓存后,我得到了该错误,并且主视图中的链接没有显示任何短代码.
我在哪里做错了?
不,那不是.错误消息来自route:cache
命令,不确定为什么清除高速缓存会自动调用此错误.
问题是使用闭包而不是控制器的路由,它看起来像这样:
// Thats the Closure
// v
Route::get('/some/route', function() {
return 'Hello World';
});
由于闭包无法序列化,因此当您有使用闭包的路由时,就无法缓存路由.
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.
I think that it's related with a route
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));
}`
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.
Where am I doing wrong?
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无法准备要序列化的路线.使用闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!