1

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.

Chaiwat Jo
  • 133
  • 4
  • 15

2 Answers2

0

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.

zuckerburg
  • 338
  • 2
  • 6
  • Hi zuckerburg, There is no Closure in web.php. But I have user(); })->middleware('auth:api'); in api.php – Chaiwat Jo Dec 20 '18 at 06:56
  • @ChaiwatJo the 'function (Request $request) {' is a Closure. See my answer in code example 1 where it says "Thats the Closure" – zuckerburg Dec 20 '18 at 07:45
  • yes, I mean this ==> user(); })->middleware('auth:api'); in api.php not web.api <==. I really no idea how to solve problem. – Chaiwat Jo Dec 20 '18 at 07:55
  • The route you specify has a Closure. Please read my answer. The error you're getting is because you're using route cache and it doesn't serialize the use of Closures and it breaks. It's in the error message you get. Please READ my answer. I specify api.php as well. That route is a Closure. GET RID OF THE CLOSURE METHOD THERE AND YOUR CODE WILL RUN. – zuckerburg Dec 20 '18 at 08:07
  • Comment out or replace the route Closure with a controller – zuckerburg Dec 20 '18 at 08:18
  • Hi zuckerburg, I'm not sure is it the related issue?. Although code commented but I still have problem. I afraid, It's route problem instead. All link show 'The requested URL /srp/landing was not found on this server.' (image https://ibb.co/tHysggf ) – Chaiwat Jo Dec 20 '18 at 08:23
  • The error you are getting now just means route is not found. Which means the question you had originally is solved. Please ask a new question with your code samples if you need any further help. – zuckerburg Dec 20 '18 at 08:32
  • No problem friend! – zuckerburg Dec 20 '18 at 08:43
  • my another thread https://stackoverflow.com/questions/53865287/laravel-route-error-404not-found-on-centos7 – Chaiwat Jo Dec 20 '18 at 09:03
0

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.

Abhishek
  • 1,543
  • 3
  • 13
  • 29
  • I got ==> $ php artisan route:cache Route cache cleared! [LogicException] Unable to prepare route [api/user] for serialization. Uses Closure. – Chaiwat Jo Dec 20 '18 at 07:08
  • @ChaiwatJo please use 'php artisan route:clear' command and don not cache the routes, and check is that works without caching? – Abhishek Dec 20 '18 at 07:19
  • Hi Abhishek, I did 'php artisan route:clear' but still problem. – Chaiwat Jo Dec 20 '18 at 07:25
  • 1
    That is because like I mentioned in my answer, route cache doesn't allow the use of Closures when defining your routes. And you clearly have a Closure – zuckerburg Dec 20 '18 at 08:05