Yii - 从另一个动作方法调用动作方法

Jas*_*ant 3 php yii

标题可能令人困惑,但这里是对代码的解释.

基于某些条件,actionContact即使用户打电话,我也可以打电话actionIndex.

解决方案:1

 public function actionIndex()
    { 
        $a = 5;
        if($a == 5){


$this->actionContact();
        }
        else{
            $this->render('index');
        }
    }

    public function actionContact()
    {
        // Some codes
        $this->render('contact');
    }
Run Code Online (Sandbox Code Playgroud)

解决方案:2

public function actionIndex()
        { 
            $a = 5;
            if($a == 5){


// Repeat code from actionContact method
            $this->render('contact');
        }
        else{
            $this->render('index');
        }
    }
Run Code Online (Sandbox Code Playgroud)

解决方案:3我可以重定向到联系网址.

我认为,解决方案1对我来说很好,我宁愿这样做.但既然我是yii的新手,我想知道,如果它的路要走?

bri*_*iiC 9

如果不是问题,你可以使用前锋

http://www.yiiframework.com/doc/api/1.1/CController#forward-detail

public function actionIndex()
{ 
    $a = 5;
    if($a == 5){
        // Forward user to action "contact"
        $this->forward('contact'); 
    }
    else{
        $this->render('index');
    }
}

public function actionContact()
{
    // Some codes
    $this->render('contact');
}
Run Code Online (Sandbox Code Playgroud)