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

chriz74's avatar

command "php artisan optimize" throwing errors re some routes w/ closures

one of the errors: Unable to prepare route [api/user] for serialization. Uses Closure.

or Unable to prepare route [/] for serialization. Uses Closure. etc

I could run the command by commenting this routes but now I get a 404 at welcome screen:

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

So how do you return a view without a closure?

0 likes
5 replies
ejdelmonico's avatar

To return a view without a closure, you add it to the relevant controller.

Route::get('test', testController@index);

// in controller
public function index()
{
    return view('test');
}
Cronix's avatar

For the middleware one, I'd create a route group, and then define individual routes within that group.

Route::middleware(['auth:api'])->group(function () {
    Route::get('/user', 'controller@method');
});

And then obviously create a controller/method that returns the authenticated user like your code is currently doing.

chriz74's avatar

@CRONIX - That one is in laravel by default. I am not using it I think. I just commented it.

Please or to participate in this conversation.