从Laravel 4.2迁移到5.0
我们的API中大约有200条路线,其中大多数是闭包。所以我花了过去几天的大部分时间将它们全部转换为控制器路线。一切都恢复稳定,但当我尝试运行php artisan route:cache
时,它会抛出异常
[Exception]
Serialization of 'Closure' is not allowed
我为function
做了一个ctrl + f,路由文件中剩下的只有两个Route::group
条目(是的,是闭包......但肯定只有路由计数。 ..)。
任何指导?在过去两天的乏味之后,这个错误......不是我想看到的。
编辑:完整的堆栈跟踪
清除路由缓存!
[Exception]
Serialization of 'Closure' is not allowed
Exception trace:
() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
serialize() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
Illuminate\Foundation\Console\RouteCacheCommand->buildRouteCacheFile() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:65
Illuminate\Foundation\Console\RouteCacheCommand->fire() at n/a:n/a
call_user_func_array() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Container\Container.php:523
Illuminate\Container\Container->call() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Console\Command.php:115
Illuminate\Console\Command->execute() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Command\Command.php:257
Symfony\Component\Console\Command\Command->run() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Console\Command.php:101
Illuminate\Console\Command->run() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Application.php:874
Symfony\Component\Console\Application->doRunCommand() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Application.php:195
Symfony\Component\Console\Application->doRun() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\symfony\console\Symfony\Component\Console\Application.php:126
Symfony\Component\Console\Application->run() at C:\Users\cjtho_000\Desktop\ecapi_l5\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:94
Illuminate\Foundation\Console\Kernel->handle() at C:\Users\cjtho_000\Desktop\ecapi_l5\artisan:36
编辑2 :删除要测试的路由组,仍然抛出异常
答案 0 :(得分:4)
事实证明,完全无益的错误消息试图告诉我,我有一个重复路由(复制粘贴错误,我有两个获取路由匹配相同的uri)。
删除副本解决了问题。
答案 1 :(得分:0)
根据您的堆栈跟踪,
vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
serialize() at vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95
Laravel正在尝试序列化路线集合on line 95 of RouteCacheCommand.php。
您获得的错误
序列化'关闭'不允许
看起来像它的PHP标准"有人试图序列化匿名函数"异常消息(相对于它可能是自定义Laravel异常)。
所有这些意味着某事正在为您的路线集添加匿名功能(即"封闭")。如果您100%确定已将其全部从routes.php
中删除,我的猜测就是第三方代码中的路径。
最好不要猜测。尝试一些类似以下
的调试代码//un-tested pseudo debugging code
//get the application's routes, using the same
//technique as the route:cache command
$app = require /path/to/your/laravel/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
$routes = $app['router']->getRoutes();
//go through each route
foreach($routes as $route)
{
//if the route is a closure
if(is_object($route) && ($route instanceof Closure))
{
//use reflection to find out which file the closure is defined in
$r = new ReflectionFunction($route);
var_dump($route->getFileName ());
}
}
这将让你弄清楚导致你的问题的闭包的位置,这应该足以指出问题。