2014-08-29 133 views
1

好了,所以我曾經有過這樣的代碼,它工作得很好:如果ELSEIF語句中的語句導致錯誤

$lastpost = ForumPos::where('user_id', '=', Auth::id())->orderby('created_at', 'desc')->first(); 

    if ($validator->fails()) 
    { 
      return Redirect::to('/forum/topic/'.$id.'/new') 
        ->withErrors($validator->messages()); 
    } 
     elseif ($lastpost->created_at->diffInSeconds() < 15) 
    { 
      return Redirect::to('/forum/topic/'.$id.'/new') 
        ->withErrors('You really need to slow down with your posting ;)'); 
    } 
     else 
    { 
      $new_thread    = new ForumThr; 
      $new_thread->topic  = $id; 
      $new_thread->user_id = Auth::id(); 
      $new_thread->title  = Input::get('title'); 
      $new_thread->save(); 

      $new_post    = new ForumPos; 
      $new_post->thread  = $new_thread->id; 
      $new_post->user_id  = Auth::id(); 
      $new_post->body   = Input::get('body'); 
      $new_post->save(); 

      return Redirect::to('/forum/thread/'.$new_thread->id.''); 
    } 

,這工作得很好,直到我發現了一點問題,所以我不得不改變這一點要得到這樣的:當我發佈一個線程,並獲得了ELSEIF語句中的if語句

$hasposted = ForumPos::where('user_id', '=', Auth::id())->count(); 

    if ($validator->fails()){ 
      return Redirect::to('/forum/topic/'.$id.'/new') 
        ->withErrors($validator->messages()); 
    } elseif ($hasposted != 0) { 
     $last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first(); 

     if ($last_post->created_at->diffInSeconds() < 15) { 
      return Redirect::to('/forum/topic/'.$id.'/new') 
        ->withErrors('You really need to slow down with your posting ;)'); 
     } 
    } else { 
      $new_thread    = new ForumThr; 
      $new_thread->topic  = $id; 
      $new_thread->user_id = Auth::id(); 
      $new_thread->title  = Input::get('title'); 
      $new_thread->save(); 

      $new_post    = new ForumPos; 
      $new_post->thread  = $new_thread->id; 
      $new_post->user_id  = Auth::id(); 
      $new_post->body   = Input::get('body'); 
      $new_post->save(); 

      return Redirect::to('/forum/thread/'.$new_thread->id.''); 
    } 

現在,我打了一個路障。我得到以下錯誤:

error #1

我只得到時候我還沒有指定的標題變量控制器使視圖得到它的這個錯誤,但不應該有一個視圖。有任何想法嗎? :S

回答

3

elseif塊(第二狀態)

if(...) 
{ 
    //first condition 
    return ...; 
} 
elseif ($hasposted != 0) { 
{ 
    //second condition  
    $last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first(); 

    if ($last_post->created_at->diffInSeconds() < 15) { 
     return Redirect::to('/forum/topic/'.$id.'/new') 
       ->withErrors('You really need to slow down with your posting ;)'); 
    } 
} 
else 
{ 
    //third condition 
    return ...;   
} 

看看當你的嵌套if語句失敗

$last_post->created_at->diffInSeconds() < 15 

該塊結束,而其餘​​條件完成而不發行Redirect。也就是說,你的嵌套if語句對第三個條件一無所知。 PHP/Laravel正在做你告訴它的事 - 告訴它做其他事情。

這純粹是一種風格建議,但我已經達到了儘可能避免多個分支條件的地步,特別是從分支內部返回時。風格更像

if(...) 
{ 
    return Redirect(); //... 
} 

if(...) 
{ 
    return Redirect(); //... 
} 

if(...) 
{ 
    return Redirect(); //... 
} 

if(...) 
{ 
    return Redirect(); //... 
}  

可能在頁面上看起來更長,但它更清楚發生了什麼事情。

If this? Do something and go away (`return`) 

Still here? Well if this-other-thing then do something and go away (`return`) 

**Still** here? Well if this-other-thing then do something and go away (`return`)  

你最終會考慮一系列是/否測試,並避免嵌套條件邏輯遇到的人爲/程序員問題。

+1

我完全同意你的款式推薦。值得注意的是,如果所有其他的失敗,你應該總是在方法底部返回一個返回值。 – 2014-08-29 17:57:05

2

在所有其他情況下,您都會重定向。如果elseif成功,但如果不成功,那麼你什麼都不做。然後嘗試使用您的主模板呈現頁面,但您尚未設置所需的任何變量。你可以通過添加另一重定向解決這個問題:

if ($last_post->created_at->diffInSeconds() < 15) { 
     return Redirect::to('/forum/topic/'.$id.'/new') 
       ->withErrors('You really need to slow down with your posting ;)'); 
    } 
    else 
    { 
     return Redirect::to('/somewhere/else/'); 
    } 
+0

當然,這不會工作?當然,它只是移動到最後的其他聲明?無論如何,這正是我所需要的,我不能只是重定向。 – 2014-08-29 17:51:20

+0

否。如果 - > elseif - > else,它將只進入最高級別的分支之一。你可能想要使用AND條件。看看剛剛發佈的其他答案。這是一個更好的方式來做到這一點。 – 2014-08-29 17:54:24

1

在Laravel IRC室討論之後,我們找到了解決方案(我相信答案在這裏就足夠了太)

最後,我想出了這個:

$hasposted = ForumPos::where('user_id', '=', Auth::id())->count(); 

    if ($validator->fails()){ 
      return Redirect::to('/forum/topic/'.$id.'/new') 
        ->withErrors($validator->messages()); 
    } elseif ($hasposted != 0) { 
     $last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first(); 

     if ($last_post->created_at->diffInSeconds() < 15) { 
      return Redirect::to('/forum/topic/'.$id.'/new') 
        ->withErrors('You really need to slow down with your posting ;)'); 
     } 
    } 

    $new_thread    = new ForumThr; 
    $new_thread->topic  = $id; 
    $new_thread->user_id = Auth::id(); 
    $new_thread->title  = Input::get('title'); 
    $new_thread->save(); 

    $new_post    = new ForumPos; 
    $new_post->thread  = $new_thread->id; 
    $new_post->user_id  = Auth::id(); 
    $new_post->body   = Input::get('body'); 
    $new_post->save(); 

    return Redirect::to('/forum/thread/'.$new_thread->id.''); 

如果它通過了所有的if語句,它會通過最終的請求,現在我很高興地說這一切都按計劃進行。謝謝,小夥子們!