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.1] Request object is not serializing #11551

Closed
SachinAgarwal1337 opened this issue Dec 27, 2015 · 5 comments
Closed

[5.1] Request object is not serializing #11551

SachinAgarwal1337 opened this issue Dec 27, 2015 · 5 comments

Comments

@SachinAgarwal1337
Copy link

I have a queued Job which accepts Request object. But I get error stating:

Serialization of 'Closure' is not allowed

Tried Debugging, found out that Request object is not getting serialized.
Plus, It is an object, why is it getting as Closure.?

@GrahamCampbell
Copy link
Member

The request object cannot be serialized. This is because it contains properties which contain objects containing closures.

@GrahamCampbell
Copy link
Member

You can get all the data out the request by writing (string) $request though.

@SachinAgarwal1337
Copy link
Author

I got it from doing $request->all()

@sjors-bootmine
Copy link

Dirty hack

 <?php
  namespace App\Support;

  use Closure;
  use stdClass;

  class SerializableRequest
  {

      public static function serialize($request)
      {
          $res = new stdClass();
          foreach ($request as $key => $value) {
              if (!$value instanceof Closure) {
                  $res->{$key} = $value;
              }
          }
          return serialize($res);
      }
      
      public static function unserialize($str)
      {
          // casts stdClass back to Request
          $className       = 'Illuminate\\Http\\Request';
          $classNameLength = strlen($className);
          return unserialize(
              preg_replace(
                  '/^O:\d+:"[^"]++"/',
                  "O:$classNameLength:\"$className\"",
                  $str
              )
          );
      }
  }

usage :

    use App\Support\SerializableRequest;
    
    $str = SerializableRequest::serialize($request);
    
    $request = SerializableRequest::unserialize($str);

@sjors-bootmine
Copy link

Please be aware that if your original request had a json content type the request object might not function as expected.
Use: $request->headers->remove('CONTENT_TYPE'); as a fix

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

3 participants