2014-08-29 105 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语句,它会通过最终的请求,现在我很高兴地说这一切都按计划进行。谢谢,小伙子们!