Exception: Serialization of 'Closure' Is Not Allowed

Exception: Serialization of 'Closure' is not allowed

Apparently anonymous functions cannot be serialized.

Example

$function = function () {
return "ABC";
};
serialize($function); // would throw error

From your code you are using Closure:

$callback = function () // <---------------------- Issue
{
return 'ZendMail_' . microtime(true) . '.tmp';
};

Solution 1 : Replace with a normal function

Example

function emailCallback() {
return 'ZendMail_' . microtime(true) . '.tmp';
}
$callback = "emailCallback" ;

Solution 2 : Indirect method call by array variable

If you look at http://docs.mnkras.com/libraries_23rdparty_2_zend_2_mail_2_transport_2file_8php_source.html

   public function __construct($options = null)
63 {
64 if ($options instanceof Zend_Config) {
65 $options = $options->toArray();
66 } elseif (!is_array($options)) {
67 $options = array();
68 }
69
70 // Making sure we have some defaults to work with
71 if (!isset($options['path'])) {
72 $options['path'] = sys_get_temp_dir();
73 }
74 if (!isset($options['callback'])) {
75 $options['callback'] = array($this, 'defaultCallback'); <- here
76 }
77
78 $this->setOptions($options);
79 }

You can use the same approach to send the callback

$callback = array($this,"aMethodInYourClass");

Serialization of 'Closure' is not allowed in laravel

The Request object has Closures as properties. You probably don't need the entire Request object but only the inputs. If you only need the inputs you can save the array of inputs:

$request->session()->put('quotation', $request->input());

quotation will contain an array of the inputs.

Exception Serialization of 'Closure' is not allowed

Seems that $items = $this->_getElementsSitemap($type); returns not serializable instance.

Your class should implement __serialize method

Serialization of 'Closure' is not allowed Laravel

Solved. The problem wasn't within the store function it's on migration. For data on CKEditor, I was using a string on migration. Changed it to text then problem solved.

All thanks to https://stackoverflow.com/a/27003432/10590832
I got an idea.

Cheers,

Laravel Jobs Serialization of 'Closure' is not allowed

Request is not serializable there is a workaround what you are trying to achieve

 public function store(StoreNewsletterRequest $request)
{
StoreNewsletterJob::dispatch($request->all());

return view('backend.dashboard.index');
}

Your job handler.

 public function handle()
{
if(!Newsletter::isSubscribed($this->request['email']))
{

Newsletter::subscribe($this->request['email'], [

config('newsletter.list_fields.firstname') => $this->request->firstname,
config('newsletter.list_fields.lastname') => $this->request->lastname

]);
}
}

Hope this helps

PHP : Serialization of a closure is not allowed

I'm guessing you are passing or getting passed a closure as the $value parameter of set().

You need to either check for and ignore closures using reflection

public function set( $key, $value)
{
if (is_object($value)) {
try {
$reflection = new ReflectionFunction($value);
if ($reflection->isClosure()) {
//Trigger a E_USER_NOTICE if you want to avoid silently failing
trigger_error("You cannot pass a closure as the second parameter of set()");
return; // Do nothing else
}
} catch (\ReflectionException $e) {
// Catch the exception thrown if $value is not a closure
}
}
$key = strtolower( $key );

$_SESSION["HA::STORE"][$key] = serialize($value);
}

Or you can catch and ignore the exception:

function set( $key, $value)
{
$key = strtolower( $key );
try{
$_SESSION["HA::STORE"][$key] = serialize($value);
} catch (\Exception $e) {
// Catch the exception thrown and handle it however you want
}
}

Or look at this github project for serializing closures

Serialization of 'Closure' is not allowed in Laravel 5.3 Email Queue

You cannot serialize request. Only eloquent model can be serialized and unserialzed. See here: https://laravel.com/docs/5.2/queues#writing-job-classes

You should use $request->all() instead of $request. Since Request is treated as closure.



Related Topics



Leave a reply



Submit