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

时间:2014-05-10 14:48:48

标签: php laravel laravel-4

这是我用来渲染我的添加项目表单的方法。我解决了生成消息Serialization of 'Closure' is not allowed的问题,但我真的不知道是什么原因造成的。更改$this->userInstance->all()的位置解决了它。删除withInput($data)也使其有效,但我无法通过重定向发送数据。

问题
所以错误已经解决了,但我想知道的是为什么下面的解决方案有效?

正确版本:

/*
 * 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);
}

错误版本:

/*
 * 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 个答案:

答案 0 :(得分:1)

可能是因为$this->userInstance->readAll()返回一个对象或对象数组,这可能是一个复杂的对象,其中包含一个或多个闭包,当你这样做时

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

Laravel必须序列化整个对象以将其(作为字符串)存储在会话var中,以便您可以在重定向的请求中访问它。在您的成功版本中,您不再重定向它,而是渲染视图。