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

afoysal's avatar

Error in php artisan route:cache

My api.php code is like below

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

I tried to run php artisan route:cache and getting error like below

https://i.stack.imgur.com/G5puX.png

0 likes
1 reply
InspiredPrynce's avatar

The problem is a route which uses a Closure instead of a controller, which looks something like this:

//                       Thats the Closure
//                             v 
Route::get('/some/route', function() {
    return 'Hello World';
});

Since Closures can not be serialized, you can not cache your routes when you have routes which use closures.

Please or to participate in this conversation.