문제

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);
}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top