laravel 无法为序列化准备路由....使用闭包 [英] laravel Unable to prepare route ... for serialization. Uses Closure

查看:53
本文介绍了laravel 无法为序列化准备路由....使用闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 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.

我哪里做错了?

推荐答案

我认为它与路线有关

I think that it's related with a route

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 无法为序列化准备路由....使用闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆