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 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性