标题可能会令人困惑,但这里有代码解释。
基于某些条件,
我可能会调用 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');
}
解决方案:2
public function actionIndex()
{
$a = 5;
if($a == 5){
// Repeat code from actionContact method
$this->render('contact');
}
else{
$this->render('index');
}
}
解决方案:3 我可以重定向到联系人 url。
我认为,解决方案 1 对我来说效果很好,我更愿意这样做。 但由于我是 yii 的新手,我想知道它是否可行?
最佳答案
如果没问题,你可以使用forward
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');
}
关于php - Yii - 从另一个 Action 方法调用一个 Action 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10084594/