
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.
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
.
Working Laravel application or a fresh installation of Laravel.
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.
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');
}
}
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!
Hope this article was helpful. You might be also interested in reading few of my other articles which will be really helpful for you
Why namespace And use Keywords Used In PHP
Lazy Load YouTube And Other Videos In Website
Setup AMP (Accelerated Mobile Pages) In PHP Laravel
Generate RSS Feeds in PHP Laravel
Custom Validation Rules In PHP Laravel (Using Artisan Command)
Increase Session Timeout In Laravel
Install Linux, Apache, MYSQL, PHP (LAMP Stack) on Ubuntu
SQLite Doesn't Support Dropping Foreign Keys in Laravel
Integrate Google Translate Into Your Website
Securely Connect Server MYSQL DB From Sequel Pro / MYSQL Workbench