Framework: Serialization of Closures is not allowed

Created on 12 Mar 2018  路  7Comments  路  Source: laravel/framework

Description:

When laravel tries to serialize an object with closures it throws an exception "Serialization of 'Closure' is not allowed". We need to be able to serialize objects with closures in order to use League Events.

Most helpful comment

All 7 comments

This is a limitation of PHP. Nothing Laravel can do about it.

don't mind me, just talking out of my ass.

The problem is not that we have an exception, but that we can't really handle it.
This throws a generic \Exception.

image

Even something as simple as this would be of help, imho.
I can handle that :)

LE: In my use case, i was trying to do a middleware that caches responses.
This was throwing exceptions in some cases.

i fixed this with the following:

try {
    $_response = '';
    $response = cache()->remember($token, $ttl, function () use ($request, $next, &$_response) {
        $_response = $next($request);
        return $_response;
    });
} catch (Throwable | Exception $e) {
    $response = $_response;
}
return $response;

I hope this is of help to you guys :)

Hi there,

Welcome to Laravel and we are glad to have you as part of the community.

Unfortunately this GitHub area is not for ideas, suggestions etc. This is only for issues/bugs with the framework code itself.

I will be closing your ticket here. You are able to open a ticket at https://github.com/laravel/ideas

Alternatively you are able to open a PR using the Contributions guidelines: https://laravel.com/docs/5.7/contributions#which-branch

If you feel I've closed this issue in error, please provide more information about how this is a framework issue, and I'll reopen the ticket.

Thanks in advance.

You can also work around this by using constructor args in your jobs with static callables. A string or a tuple (of strings).

So long as serialize in Illuminate\Queue\Queue::createObjectPayload doesn't eat it, you should be ok.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lzp819739483 picture lzp819739483  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

kerbylav picture kerbylav  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

JamborJan picture JamborJan  路  3Comments