Question

[Solved] laravel Unable to prepare route … for serialization. Uses Closure

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.

I think that it’s related with a route

Route::get('/article/{slug}', '[email protected]');

associated with a particular method in my controller:

public function slug($slug) {
    $article = Article::where('slug',$slug)->first();

    $id = $article ->id_article ;

    if ( ($article=== null) || (is_null($id)) ) return view('errors/Db');

    else return view('detail')->with(array('article'=> $article,  'title'=>'My title - '.$article->title)); 
}`

In short, from a master view I pass $slug, that is a shortlink to the article, with $slug , which is unique in the database, I identify the record and then I pass it’s contents to the detail view.

I didn’t have any problem when I wrote the method, infact it worked like a charm, but after I cleaned caches, I get that error and the links in the master view don’t show any shortcode.

Where am I doing wrong?

Solution #1:

I think that it’s related with a route

Route::get('/article/{slug}', '[email protected]');

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.

Respondent: tkausl

Solution #2:

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

routes/api.php

Laravel has a default auth api route in the above file.

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

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

Respondent: Manpreet

Solution #3:

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 routesapi.php code with some thing like :

Route::middleware('auth:api')->get('/user', '[email protected]');

and in UserController put this method:

 public function AuthRouteAPI(Request $request){
    return $request->user();
 }
Respondent: IBRAHIM EZZAT

Solution #4:

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

Respondent: Akram Chauhan

Solution #5:

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.

Respondent: Pawan Verma

Solution #6:

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

Respondent: stilo bit

Solution #7:

If someone is still looking for an answer, for me the problem was in routes/web.php file. Example:

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

It is also Route, so yeah…Just remove it if not needed and you are good to go!
You should also follow answers provided from above.

Respondent: DM developing

Solution #8:

check that your web.php file has this extension

use IlluminateSupportFacadesRoute;

my problem gone fixed by this way.

Respondent: pankaj kumar

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy