无法准备route [api/user]进行序列化.使用闭包-Laravel [英] Unable to prepare route[api/user] for serialisation. Uses Closure - Laravel

查看:141
本文介绍了无法准备route [api/user]进行序列化.使用闭包-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LogicException  : Unable to prepare route [api/user] for serialization. Uses Closure.

  at /var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:917
    913|      */
    914|     public function prepareForSerialization()
    915|     {
    916|         if ($this->action['uses'] instanceof Closure) {
  > 917|             throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
    918|         }
    919| 
    920|         $this->compileRoute();
    921| 

  Exception trace:

  1   Illuminate\Routing\Route::prepareForSerialization()
      /var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php:62

  2   Illuminate\Foundation\Console\RouteCacheCommand::handle()
      /var/www/html/dev_laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32

当我尝试运行laravel命令

When i trying to run the laravel command

php artisan route:cache

我尝试找出解决方案,但没有找到正确的解决方案. https://github.com/laravel/framework/issues/22034

I try to find out the solution but not get the correct solution. https://github.com/laravel/framework/issues/22034

这个laravel错误还是仍然存在?

Is this laravel bug still or fixed the bug?

我在web.php文件中有代码

I have the code on web.php file

Route::get('/', function () {
    return view('welcome');
});
Route::resource('photos', 'PhotoController@index');

我正在使用Laravel 5.8.刚刚安装并迁移了数据库.我是laravel的初学者.

I'm Using Laravel 5.8. Just installed and migrated database. I'm beginner for laravel.

任何人都可以让我知道正确的解决方案吗?

Can anyone let me know the correct solution?

预先感谢

推荐答案

这不是错误.如果要缓存路由,则不能使用基于Closure的路由.直接将任何基于Closure的路由定向到Controller方法,您会没事的.[您在 web.php 中有一个,并且错误指出了在 api.php 中的一个.]

It is not a bug. You can not use Closure based routes like that if you want to cache the routes. Direct any Closure based route to a Controller method instead and you will be fine. [You have one in web.php and the error is pointing out one in api.php]

您在 web.php 中使用的基于Closure的路由可以替换为:

The Closure based route you have in web.php could be replaced with:

Route::view('/', 'welcome');

这会将其定向到仅处理返回的视图文件的Controller.

This would direct it to a Controller that just handles returning view files.

api.php 中基于Closure的路由应指向控制器:

The Closure based route in api.php should point to a Controller:

Route::middleware('auth:api')->get('user', 'SomeController@user');

laravel/laravel 项目随附的所有路线都视为可正常使用,但出于演示目的而存在.

Consider any routes that come with the laravel/laravel project as being functional but are there for demonstration purposes.

不能缓存基于Closure的路由.要使用路由缓存,必须将所有Closure路由转换为控制器类."

"Closure based routes cannot be cached. To use route caching, you must convert any Closure routes to controller classes."

Laravel 5.8-文档-控制器-路由缓存

这篇关于无法准备route [api/user]进行序列化.使用闭包-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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