2014-05-10 59 views
0

這是我用來呈現我的添加項目窗體的方法。我解決了生成消息Serialization of 'Closure' is not allowed,但我真的不知道是什麼原因造成的。更改$this->userInstance->all()的位置解決了它。刪除withInput($data)也使它的工作,但後來我不能發送數據與我的重定向。在Laravel 4中不允許「封閉」的序列化

問題
所以錯誤已經被解決了,但我想知道究竟是爲什麼呢下面的作品該如何解決?

正確版本:

/* 
* 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

大概是因爲$this->userInstance->readAll()返回一個或多個對象的陣列中,這可能是在它的一個或多個封閉件一個複雜的對象,並當你做

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

拉拉維爾將不得不序列化整體e對象將其作爲字符串存儲在會話變量中,因此您可以在重定向的請求中訪問它。在你的成功版本中,你不再重定向它,而是渲染一個視圖。

+0

是的,你說得對,那簡直是愚蠢的。用戶只需使用make,但我也將它們添加到重定向中。謝謝! – sidneydobber

相關問題