Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.0]route:cache with Closure problem #7319

Closed
stevenyangecho opened this issue Feb 7, 2015 · 9 comments
Closed

[5.0]route:cache with Closure problem #7319

stevenyangecho opened this issue Feb 7, 2015 · 9 comments

Comments

@stevenyangecho
Copy link

command artisan route:cache

when met closure route:

Route::get('/', function()
{
    return 'Hello World';
});

it will fail with

exception 'LogicException' with message 'Unable to prepare route [/] for serialization. Uses Closure.'
@stevenyangecho stevenyangecho changed the title route:cache with Closure problem [5.0]route:cache with Closure problem Feb 7, 2015
@GrahamCampbell
Copy link
Member

Yep, that's the expected behaviour.

@GrahamCampbell
Copy link
Member

Laravel's source code:

    /**
     * Prepare the route instance for serialization.
     *
     * @return void
     *
     * @throws LogicException
     */
    public function prepareForSerialization()
    {
        if ($this->action['uses'] instanceof Closure)
        {
            throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
        }

        unset($this->container);

        unset($this->compiled);
    }

@stevenyangecho
Copy link
Author

but ,when the app has some closure route,it means cant route cache?
has solution ?
change the app code to not use closure route,or do some others like skip cache for closure route but cache for normals?

@GrahamCampbell
Copy link
Member

Basically, you cannot use route caching at all if you have closures in your routes. Selective caching would defeat the object.

@stevenyangecho
Copy link
Author

ok,thanks,so I will change code to not use closure route at all

@rogue780
Copy link

It is possible, with a little creativity, to serialize a Closure. The code should be modified to allow this. I might have to submit a PR for it

@crynobone
Copy link
Member

AFAIK, taylor tried it before and found out that having to serialize and unserialize closure reduce the performance that you could possibly gain from using route caching.

@codemdpro
Copy link

If you're not using any api func. , you can comment
/*
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
*/
in routes/api.php

@devcircus
Copy link
Contributor

Old thread but those are just sample routes. Delete them. This is covered in the docs.

wi-wissen added a commit to wi-wissen/InstaHub that referenced this issue Apr 23, 2019
wi-wissen added a commit to wi-wissen/InstaHub that referenced this issue Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants