如果elseif语句中的语句导致错误

时间:2014-08-29 17:43:00

标签: php laravel

好的,所以我以前有这个代码,它运行良好:

    $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.'');
    }

这个工作正常,直到我注意到一个小问题所以我不得不改变这一点来得到这个:

    $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.'');
    }

现在,当我发布一个帖子并转到elseif语句中的if语句时,我遇到了障碍。我收到以下错误:

error #1

当我没有在控制器中指定title变量以便视图获取它时,我只会收到此错误,但是不应该有视图。有任何想法吗? :S

3 个答案:

答案 0 :(得分: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 :(得分:2)

在所有其他条件下,您都会进行重定向。如果elseif成功,但if不成功那么你什么都不做。然后它尝试使用您的主模板呈现页面,但您没有设置它需要的任何变量。你可以通过添加另一个重定向来解决这个问题:

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

答案 2 :(得分: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语句,它将完成最后的请求,现在我很高兴地说它一切按计划运行。谢谢,小伙子们!