Home > Back-end >  Laravel 7.2.5 Unable to prepare route [api/user] for serialization. Uses Closure
Laravel 7.2.5 Unable to prepare route [api/user] for serialization. Uses Closure

Time:03-06

I was facing the issue when I do php artisan optimize. Below is my api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */

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

I am confuse & don't know what I am missing. Already tried following options

  1. Delete all cache files
  2. Also tried php artisan cache:clear
  3. Composer update / composer dump-autoload
  4. env file changes CACHE_DRIVER=file,SESSION_DRIVER=database

Thanks in advance .

CodePudding user response:

If you check Laravel's optimize command:

    /**
 * Execute the console command.
 *
 * @return void
 */
public function handle()
{
    $this->call('config:cache');
    $this->call('route:cache');

    $this->info('Files cached successfully!');
}

There is a line $this->call('route:cache'); This line is throwing the error.

Laravel is trying to cache the routes. It does not accept Closure while caching the route. That's why moving your code to a controller fixed the issue.

  • Related