【问题标题】:Laravel "Serialization of 'Closure' is not allowed"Laravel“不允许序列化'闭包'”
【发布时间】:2014-10-06 00:50:03
【问题描述】:

当我在 Laravel 中存储数据集时,有时会收到此错误,但尚未找到解决方案。

Serialization of 'Closure' is not allowed
Open: ./vendor/laravel/framework/src/Illuminate/Session/Store.php
     */
    public function save()
    {
        $this->addBagDataToSession();

        $this->ageFlashData();

        $this->handler->write($this->getId(), serialize($this->attributes));

        $this->started = false;

这是发生错误时调用的函数:

public function store()
    {

        $data = Input::all();
        $validator = array('first_name' =>'required', 'last_name' => 'required', 'email' => 'email|required_without:phone', 'phone' => 'numeric|size:10|required_without:email', 'address' => 'required');
        $validate = Validator::make($data, $validator);
        if($validate->fails()){
            return Redirect::back()->with('message', $validate);
        } else {
            $customer = new Customer;
            foreach (Input::all() as $field => $value) {
                if($field == '_token') continue;
                $customer->$field = $value;
            }
            $customer->save();
            return View::make('admin/customers/show')->withcustomer($customer);
        }
    }

是什么导致了这个序列化错误?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    只需替换以下行:

    return Redirect::back()->with('message', $validate);
    

    用这个:

    return Redirect::back()->withErrors($validate);
    

    此外,您可以使用类似这样的方法(使用旧值重新填充表单):

    return Redirect::back()->withErrors($validate)->withInput();
    

    view 中,您可以使用$errors 变量来获取错误消息,因此如果您使用$errors->all(),那么您将收到一系列错误消息,并且要获得特定错误,您可以尝试类似这样的操作:

    {{ $errors->first('email') }} // Print (echo) the first error message for email field
    

    另外,在以下行中:

    return View::make('admin/customers/show')->withcustomer($customer);
    

    您需要将动态方法更改为withCustomer 而不是withcustomer,这样您就可以在view 中访问$customer 变量。

    【讨论】:

      【解决方案2】:
      return Redirect::back()->with('message', $validate);
      

      你告诉 Laravel 将整个验证器对象序列化到会话中。要重定向错误,请使用withErrors 方法:

      return Redirect::back()->withErrors($validate);
      

      这会将错误消息从验证器中取出,并在重定向之前将它们闪现到会话中。你现在这样做的方式是试图将整个类存储在 Session 中,这会导致你的错误。

      我看到的另一个问题是我认为View 类上没有withcustomer 方法:

      return View::make('admin/customers/show')->withcustomer($customer);
      

      尝试将其更改为 with:

      return View::make('admin/customers/show')->with('customer', $customer);
      

      或确保将Customer 部分大写:

      return View::make('admin/customers/show')->withCustomer($customer);
      

      另见this question

      【讨论】:

      • 其实return View::make('admin/customers/show')->with($customer);是不行的,OP用过withcustomer但应该是withCustomer,所以$customer变量会在view中可用,它是@987654338 @.
      • 很高兴知道。我修复了我错过的几个小问题。我以前没见过with 的用法,所以我有点迷茫。
      猜你喜欢
      • 2014-06-20
      • 1970-01-01
      • 2020-04-06
      • 2018-05-08
      • 2014-04-21
      • 2019-01-30
      • 2017-02-14
      • 2012-11-23
      • 1970-01-01
      相关资源
      最近更新 更多