laravel 无法准备路由......以进行序列化。使用闭包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45266254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel Unable to prepare route ... for serialization. Uses Closure
提问by Francesco
When I clear caches in my Laravel 5.2 project, I see this error message:
当我清除 Laravel 5.2 项目中的缓存时,我看到以下错误消息:
[LogicException] Unable to prepare route [panel] for serialization. Uses Closure.
[LogicException] 无法为序列化准备路由 [面板]。使用闭包。
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.
简而言之,从主视图中,我传递 $slug ,这是文章的短链接, $slug 在数据库中是唯一的,我识别记录,然后将其内容传递给详细信息视图。
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?
我哪里做错了?
回答by tkausl
I think that it's related with a route
Route::get('/article/{slug}', 'Front@slug');
associated with a particular method in my controller:
我认为这与路线有关
Route::get('/article/{slug}', 'Front@slug');
与我的控制器中的特定方法相关联:
No, thats not it. The error message is coming from the route:cache
command, not sure why clearing the cache calls this automatically.
不,不是这样。错误消息来自route:cache
命令,不知道为什么清除缓存会自动调用它。
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.
因为闭包不能被序列化,所以当你有使用闭包的路由时你不能缓存你的路由。
回答by Manpreet
If none of your routes contain closures, but you are still getting this error, please check
如果您的路线都没有包含闭包,但您仍然收到此错误,请检查
routes/api.php
路线/api.php
Laravel has a default auth api route in the above file.
Laravel 在上述文件中有一个默认的 auth api 路由。
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which can be commented or replaced with a call to controller method if required.
如果需要,可以对其进行注释或替换为对控制器方法的调用。
回答by IBRAHIM EZZAT
This is definitely a bug.Laravel offers predefined code in routes/api.php
这绝对是一个错误。Laravel 在 routes/api.php 中提供了预定义的代码
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which is unabled to be processed by:
无法通过以下方式处理:
php artisan route:cache
This definitely should be fixed by Laravel team.(check the link),
这绝对应该由 Laravel 团队修复。(检查链接),
simply if you want to fix it you should replace routes\api.php code with some thing like :
只是如果你想修复它,你应该用一些类似的东西替换 routes\api.php 代码:
Route::middleware('auth:api')->get('/user', 'UserController@AuthRouteAPI');
and in UserController put this method:
并在 UserController 中放置此方法:
public function AuthRouteAPI(Request $request){
return $request->user();
}
回答by Pawan Verma
Check your routes/web.phpand routes/api.php
检查你的routes/web.php和routes/api.php
Laravel comes with default route closure in routes/web.php:
Laravel 在 routes/web.php 中带有默认的路由关闭:
Route::get('/', function () {
return view('welcome');
});
and routes/api.php
和路由/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
if you remove that then try again to clear route cache.
如果您删除它,请再次尝试清除路由缓存。
回答by DM developing
If someone is still looking for an answer, for me the problem was in routes/web.php file. Example:
如果有人仍在寻找答案,对我来说问题出在 routes/web.php 文件中。例子:
Route::get('/', function () {
return view('welcome');
});
It is also Route, so yeah...Just remove it if not needed and you are good to go! You should also follow answers provided from above.
它也是路线,所以是的...如果不需要,只需将其删除,您就可以开始了!您还应该遵循上面提供的答案。
回答by stilo bit
the solustion when we use routes like this:
当我们使用这样的路由时的解决方案:
Route::get('/', function () {
return view('welcome');
});
laravel call them Closure so you cant optimize routes uses as Closures you must route to controller to use php artisan optimize
laravel 称它们为 Closure,因此您无法优化路由使用作为 Closures 您必须路由到控制器才能使用 php artisan optimize