1
votes

I created laravel project that work fine on my local windows pc. Once I upload to Centos7 server(via SSH), all my routes did not working. I tried to fix by action e.g clear cache, delete vendor folder and redo install composer but nothing help. I grab from log as below.

[2018-12-20 13:09:17] local.ERROR: LogicException: Unable to prepare route [api/user] for serialization. Uses Closure. in /var/www/html/srp/vendor/laravel/framework/sr$ Stack trace:

#0 /var/www/html/srp/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php(61): Illuminate\Routing\Route->prepareForSerialization()

#1 [internal function]: Illuminate\Foundation\Console\RouteCacheCommand->fire()

#2 /var/www/html/srp/vendor/laravel/framework/src/Illuminate/Container/Container.php(508): call_user_func_array(Array, Array)

#3 /var/www/html/srp/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)

#4 /var/www/html/srp/vendor/symfony/console/Command/Command.php(261): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Sym$

#5 /var/www/html/srp/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\$

#6 /var/www/html/srp/vendor/symfony/console/Application.php(817): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Com$

#7 /var/www/html/srp/vendor/symfony/console/Application.php(185): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Foundation\Console\RouteCacheCo$

#8 /var/www/html/srp/vendor/symfony/console/Application.php(116): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Objec$

#9 /var/www/html/srp/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(121): Symfony\Component\Console\Application->run(Object(Symfony\Component\Co$

#10 /var/www/html/srp/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Out$ #11 {main}

Appreciated much for all advice, thanks.

2

2 Answers

0
votes

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 route:cache your routes when you have routes which use Closures. And this is why you see that error. Clearing the cache or routes won't work because this is a compile-time error.

If none of your routes contain closures, but you are still getting this error, please check

routes/api.php

Laravel adds a default auth api route to above file.

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

which can be commented out or replaced with a call to controller method if required

To replace with a controller:

 Route::middleware('auth:api')->get('/user', 'UserController@index');

If you don't need it, comment it out.

0
votes

Below link can give you a better idea,

laravel Unable to prepare route ... for serialization. Uses Closure

Error is due to the route:cache command being called and your routes may be having some closures there, so avoid route:cache that may be real cause.