当我清除Laravel 5.2项目中的缓存时,我看到以下错误消息:

[LogicException] Unable to prepare route [panel] for serialization. Uses Closure.

我认为这与路由有关

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)); 
}`

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:

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相关问答推荐

如何使用 laravel 更新单个值

如何为 CMP 横幅安装谷歌同意脚本?

try 发布 AJAX 请求时 POST 405(方法不允许)-Laravel 4

如何使用 Laravel 查询生成器在 WHERE 条件周围添加括号

laravel Eloquent 模型更新事件未触发

Laravel - 如果值包含某个字符串(取自搜索输入),则查询模型

Laravel 5.2 无法打开 laravel.log

Laravel 5 - ErrorException 无法打开流:权限被拒绝

Laravel 框架类在 PHPUnit 数据提供程序中不可用

SQLSTATE [01000]:警告:1265 列的数据被截断(truncated)

为什么客户凭证应该与 Laravel Passport 中的用户相关联?

测试指向外部站点的重定向链接

Laravel Eloquent - 加入表时防止覆盖值

Select,where JSON 包含数组

在 Laravel 5.4 中将文件存储在公共目录和存储中的区别

如何获取 Laravel 块的返回值?

如何使 Laravel (Blade) 文本字段只读

是否可以在 Laravel 的不同数据库中引用外键?

laravel:如何获取 eloquent 模型的列名?

Laravel 5.3 - 用户Collection 的单一通知(关注者)