108

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}', 'Front@slug');

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?

Francesco
  • 2,042
  • 2
  • 19
  • 29
  • Please [edit](https://stackoverflow.com/posts/45266254/edit) your question to include the rest of your routes (especially the panel route) – Chris Forrence Dec 06 '17 at 22:04

11 Answers11

160

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.

tkausl
  • 13,686
  • 2
  • 33
  • 50
  • 3
    so the command => php artisan route:cache not working on laravel? a bug? – robspin Oct 09 '18 at 07:21
  • 3
    @robspin I don't think it's a bug, since it's explicitly said in Laravel docs https://laravel.com/docs/5.7/deployment#optimization (check section Optimizing Route Loading) – Anatoliy Arkhipov Oct 21 '18 at 10:37
  • 4
    Since this feature uses PHP serialization, you may only cache the routes for applications that exclusively use controller based routes. PHP is not able to serialize Closures. So not use the php artisan route:cache is the right way! Just use the php artisan cache:clear – robspin Jan 09 '19 at 13:41
  • 2
    so, any solution ? – questionasker Jun 28 '19 at 10:38
  • 1
    see IBRAHIM EZZAT solution below. Essentially need to replace any closures with controller methods in your route files – Alistair R Jul 30 '19 at 12:18
  • Ya know.... I probably wouldn't have figure that one out. This makes me curious if anyone has come up with library to serialize closures. – Jed Lynch Feb 28 '20 at 19:20
64

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.

Manpreet
  • 2,450
  • 16
  • 21
  • This is the right answer, when I comment it out the Route/api.php Route functions, this error is gone. Thank you Manpreet. – li bing zhao Feb 19 '21 at 13:52
52

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();
 }
IBRAHIM EZZAT
  • 964
  • 9
  • 22
  • 3
    Seems like a bug indeed. If they make it possible to use closures in routes then they should fix the artisan commands so that they at least don't give any errors. – Arno van Oordt Jul 20 '20 at 13:55
  • Here's the relevant PR, which was closed :( https://github.com/laravel/laravel/pull/4601 – William Turrell Aug 11 '20 at 19:29
11

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

Akram Chauhan
  • 1,006
  • 1
  • 12
  • 19
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

stilo bit
  • 144
  • 1
  • 2
6

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.

Pawan Verma
  • 1,152
  • 14
  • 22
6

If you're coming to this problem because you've upgraded Laravel <5.8 project up to >=5.8, you've likely used the ./vendor/bin/carbon-upgrade method to upgrade the project as suggested by your terminal. In this case, you simply need to remove the following two blocks from the bottom of your composer.json file and composer install again:

    "post-install-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postInstall",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan optimize"
    ],
Luc Boone
  • 75
  • 1
  • 5
4

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.

DM developing
  • 423
  • 5
  • 15
0

In order to troubleshoot this (at least in laravel 6): The action property inside Route.php has all the info needed. A better error message should be possible to provide by laravel.

What I did was to add a dd($this->action) just before the exception is thrown here: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Routing/Route.php#L917

With that in place I could easily pinpoint the location, in my case api.php and lines 22-24:

array:6 [
  "middleware" => "api"
  "domain" => "local-api.mydomain.com"
  "uses" => Closure()^ {#6497
    class: "App\Providers\RouteServiceProvider"
    this: App\Providers\RouteServiceProvider {#5743 …}
    file: "./routes/api.php"
    line: "22 to 24"
  }
  "namespace" => "App\Http\Controllers"
  "prefix" => null
  "where" => []
]
Swaner
  • 51
  • 4
0

This is how I solved mine.

  1. Navigate to routes directory

  2. Then open api.php

  3. comment code that looks like this: Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });

  4. Then run php artisan optimize

Joshua Johns
  • 340
  • 1
  • 3
  • 10
-3

check that your web.php file has this extension

use Illuminate\Support\Facades\Route;

my problem gone fixed by this way.

pankaj
  • 1
  • 17
  • 36