Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Unable to prepare route [{fallbackPlaceholder}] for serialization. Uses Closure In Laravel


Share On     Share On WhatsApp     Share On LinkedIn


In your Laravel production environment you will be doing php artisan route:cache but sometimes you will come across a wired error that goes something like the following


Unable to prepare route [{fallbackPlaceholder}] for serialization. Uses Closure.

 at vendor/laravel/framework/src/Illuminate/Routing/Route.php:1056
  1052|   */
  1053|   public function prepareForSerialization()
  1054|   {
  1055|     if ($this->action['uses'] instanceof Closure) {
 > 1056|       throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
  1057|     }
  1058| 
  1059|     $this->compileRoute();
  1060| 

   +15 vendor frames 
 16 artisan:37
   Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))


In this article you will learn how to resolve this issue in Laravel.


Prerequisites


Working Laravel application or a fresh installation of Laravel.


Why This Problem Occurs?


One of the basic question is to check why does this problem occur. After a careful observation of the error you will get to know that in you web.php or api.php you might have used closure routes like the following


/** Fallback Route */
Route::fallback(function () {
    return view('errors.404');
});


Nothing wrong in the working of the above code. Its just a closure inside your route. The problem comes when you push the same closure route to your production.


When you run the following command in your production


php artisan route:cache


You will get the error shown in the intro of this article.



How To Resolve It? Here Is The Solution


Already you know that you get this error because of closure in your routes. So avoid using it by the following way


/** Fallback Route */
Route::fallback("ErrorsController@show404");


Create ErrorsController you can rename it to whatever you need and even the name of the method from show404 to other name as per your needs.


php artisan make:controller ErrorsController


In your ErrorsController add the following code


class ErrorsController extends Controller
{
    public function show404()
    {
        return view('errors.404');
    }
}



Testing Code & Re-Running route:cache


Now, Once you add the above code. Push the code to production and re run the route:cache command like the following


php artisan route:cache


You will be able to see the success message


Route cache cleared!
Routes cached successfully!

Conclusion


Hope this article was helpful. You might be also interested in reading few of my other articles which will be really helpful for you







Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing