douyun8901 2014-05-10 14:48
浏览 42
已采纳

Laravel 4中不允许序列化'Closure'

This is the method I'm using to render my add project form. I solved the that generated the message Serialization of 'Closure' is not allowed, but I don't really know what caused it. Changing the location of the $this->userInstance->all() solved it. Removing the withInput($data) also made it work, but then I wouldn't be able to send data with my redirect.

Question
So the error has been solved already, but what I want to know is why does the solution below works?

Correct version:

/*
 * Add project.
 *
 **********************************************************************************************************/

public function addProject() {
    /*
     * When the user is not authorized for the page show unauthorized page.
     */
    if(Atomend\Aeuser\AEUser::authorize('add-project') == false) {
        return View::make("unauthorized");
    }
    /*
     * Initialize the data array that gets send to the view.
     */
    $data = [
        "errors" => null
    ];
    /*
     * When the form is posted.
     */
    if(Input::server("REQUEST_METHOD") == "POST") {
        /*
         * If the input is valid set the credentials.
         */
        if($this->projectInstance->create()) {
            /*
             * Set the status message and set the status to true.
             */
            $data["successes"] = $this->projectInstance->successes();
            /*
             * Redirect to the user overview on success.
             */
            return Redirect::to("/projects")
                ->withInput($data);
        } else {
            /*
             * Flash the input for usage in the view.
             */
            Input::flash();
            /*
             * Set the data errors variable.
             */
            $data["errors"] = $this->projectInstance->errors();
        }
    }
    /*
     * Get all users.
     */
    $data["users"] = $this->userInstance->readAll();

    return View::make("project-add", $data);
}

Error version:

/*
 * Add project.
 *
 **********************************************************************************************************/

public function addProject() {
    /*
     * When the user is not authorized for the page show unauthorized page.
     */
    if(Atomend\Aeuser\AEUser::authorize('add-project') == false) {
        return View::make("unauthorized");
    }
    /*
     * Initialize the data array that gets send to the view.
     */
    $data = [
        "errors" => null
    ];
    /*
     * Get all users.
     */
    $data["users"] = $this->userInstance->readAll();
    /*
     * When the form is posted.
     */
    if(Input::server("REQUEST_METHOD") == "POST") {
        /*
         * If the input is valid set the credentials.
         */
        if($this->projectInstance->create()) {
            /*
             * Set the status message and set the status to true.
             */
            $data["successes"] = $this->projectInstance->successes();
            /*
             * Redirect to the user overview on success.
             */
            return Redirect::to("/projects")
                ->withInput($data);
        } else {
            /*
             * Flash the input for usage in the view.
             */
            Input::flash();
            /*
             * Set the data errors variable.
             */
            $data["errors"] = $this->projectInstance->errors();
        }
    }
    return View::make("project-add", $data);
}
  • 写回答

1条回答 默认 最新

  • drpqxogph15436713 2014-05-10 15:02
    关注

    Probably because $this->userInstance->readAll() returns an object or array of objects, this is probably a complex object with one or more closures in it, and when you do

    return Redirect::to("/projects")->withInput($data);
    

    Laravel will have to serialize the whole object to store it (as string) in a session var, so you have access to it in the redirected request. In your successful version of it you are not redirecting it anymore, but rendering a view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?