laravel Unable to prepare route … for serialization. Uses Closure

前端 未结 8 1782
心在旅途
心在旅途 2020-11-30 04:48

When I clear caches in my Laravel 5.2 project, I see this error message:

[LogicException] Unable to prepare route [panel] for serialization. Uses Closure.

相关标签:
8条回答
  • 2020-11-30 04:54

    the solustion when we use routes like this:

    Route::get('/', function () {
        return view('welcome');
    });
    

    laravel call them Closure so you cant optimize routes uses as Closures you must route to controller to use php artisan optimize

    0 讨论(0)
  • 2020-11-30 05:01

    This is definitely a bug.Laravel offers predefined code in routes/api.php

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

    which is unabled to be processed by:

    php artisan route:cache
    

    This definitely should be fixed by Laravel team.(check the link),

    simply if you want to fix it you should replace routes\api.php code with some thing like :

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

    and in UserController put this method:

     public function AuthRouteAPI(Request $request){
        return $request->user();
     }
    
    0 讨论(0)
  • 2020-11-30 05:05

    check that your web.php file has this extension

    use Illuminate\Support\Facades\Route;
    

    my problem gone fixed by this way.

    0 讨论(0)
  • 2020-11-30 05:10

    The Actual solution of this problem is changing first line in web.php

    Just replace Welcome route with following route

    Route::view('/', 'welcome');
    

    If still getting same error than you probab

    0 讨论(0)
  • 2020-11-30 05:12

    Check your routes/web.php and routes/api.php

    Laravel comes with default route closure in routes/web.php:

    Route::get('/', function () {
        return view('welcome');
    });
    

    and routes/api.php

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

    if you remove that then try again to clear route cache.

    0 讨论(0)
  • 2020-11-30 05:15

    I think that it's related with a route

    Route::get('/article/{slug}', 'Front@slug');
    

    associated with a particular method in my controller:

    No, thats not it. The error message is coming from the route:cache command, not sure why clearing the cache calls this automatically.

    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 cache your routes when you have routes which use closures.

    0 讨论(0)
提交回复
热议问题