Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GodziLaravel's avatar

why php artisan optimize returns :LogicException : Unable to prepare route [api/user] for serialization. Uses Closure.

Hello,

I try to php artisan optimize, but I got this error message :

vagrant@homestead:~/code/crm$ php artisan optimize
Configuration cache cleared!
Configuration cached successfully!
Route cache cleared!

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

  at /home/vagrant/code/crm/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()
      /home/vagrant/code/crm/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php:62

  2   Illuminate\Foundation\Console\RouteCacheCommand::handle()
      /home/vagrant/code/crm/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32

  Please use the argument -v to see more details.

api.php

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| 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!
|
*/

Auth::routes();

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


In web.php I use multiple routes starting by api/...

...

Route::group(['middleware' => ['auth'], 'as' => 'adminManagement'], function () {
    Route::resource('api/permissions', 'PermissionController', [
        'only' => ['index', 'show', 'store', 'update', 'destroy']
    ])->middleware('admin');


    Route::resource('api/groups', 'GroupController', [
        'only' => ['index', 'show', 'store', 'update', 'destroy']
    ])->middleware('admin');


    Route::resource('api/members', 'UserController', [
        'only' => ['index', 'show', 'store', 'update', 'destroy']
    ])->middleware('admin');
...
0 likes
2 replies
fylzero's avatar

@mostafalaravel I know I'm not answering the question exactly... but this according to the Laravel 5.5 upgrade guide...

"With recent improvements to PHP op-code caching, the optimize Artisan command is no longer needed. You should remove any references to this command from your deployment scripts as it will be removed in a future release of Laravel."

https://laravel.com/docs/5.5/upgrade

Essentially, as of Laravel 5.5, which requires PHP 7+... PHP 7+ has improved caching and no longer needs the optimize command. I think it just exists as output to not break builds that were upgraded... but I don't think it actually does anything anymore.

23 likes
Nakov's avatar

@mostafalaravel I think you are facing a problem discussed here:

https://github.com/laravel/framework/issues/22034

So basically you have a route with a closure:

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

which cannot be cached. So remove that route, or use a controller instead of closure and the error should be gone. :)

Please or to participate in this conversation.