3
votes
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

When i trying to run the laravel command

php artisan route:cache

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

Is this laravel bug still or fixed the bug?

I have the code on web.php file

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

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

Can anyone let me know the correct solution?

Thanks in advance

3

3 Answers

2
votes

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]

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

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

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

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

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

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

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

Laravel 5.8 - Docs - Controllers - Route Caching

1
votes

EDIT:

AS OF LARAVEL 8.X YOU CAN ALSO CACHE CLOSURE BASED ROUTES

When you run the command php artisan route:cache

Laravel will cache all your routes and store it in specified Cache Driver

Now Comming to your Error Message:

As the error Message Clearly Says that

Closure Routes can't be Cached

And even the Laravel Docs says that Route Caching

By Default Laravel Comes with Four routes files

And this will have 2 Closure based routes

Solution:

  1. You can remove them if you no longer using that routes
  2. Make a Controller and Move that to Controller

LogicException : Unable to prepare route [api/user]

Which means that

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

This Code in Your routes/api.php is causing the issue So

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

Or you can remove that if not using that

0
votes

You could not use specific method when you use resource for the routing.

You can use

Route::resource('photos', 'PhotoController');

Or

Route::get('photos', 'PhotoController@index');