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

devionti's avatar

Problem with optimiziation

So I am trying to optimize my application but it says it is using closure. I checked web.php and all the routes point to a controller

Web.php

Route::get('/',['as' => 'site.home', 'uses' => 'BlogController@index']);
Route::get('contact', ['as'=>'site.contact', 'uses' => 'BlogController@contact']);

Controller

 public function index()
    {
        if(Session::get('locale') == null)
        {
            Session::put('locale', 'en');
        }

        $posts = Posts::with('publishUser')->orderby('created_at', 'desc')->limit(3)->get();

        return view('pages.home', ['posts' => $posts]);
    }

    public function contact(){
        return view('pages.contact');
    }

After Running

php artisan optimize

I get this

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

  at vendor/laravel/framework/src/Illuminate/Routing/Route.php:1150
    1146|      */
    1147|     public function prepareForSerialization()
    1148|     {
    1149|         if ($this->action['uses'] instanceof Closure) {
  > 1150|             throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
    1151|         }
    1152|
    1153|         $this->compileRoute();
    1154|

I am using Laravel Nova as well. So I am not sure where can I find all the routing so that I can change them for optimisation.

0 likes
5 replies
Sinnbeck's avatar

Try checking api.php in the routes directory

devionti's avatar

Api.php

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

Commenting it does the trick

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

There you go. Either remove it or change it to use a controller

 Route::middleware('auth:api')->get('/user', 'UserController@show');
Sinnbeck's avatar

If the issue was solved, please remember to mark a best answer to set the thread as solved :)

1 like

Please or to participate in this conversation.