Php yii2中的afterSave重定向

Php yii2中的afterSave重定向,php,yii2,Php,Yii2,我的模型中有一个afterSave函数,它根据用户给定的开始时间和持续时间保存某些服务的到期日期。我的afterSave工作正常,但在保存模型后,它不会被重定向,而是显示一个空白页面 型号: public function afterSave($insert) { $month= "+".$this->duration_in_months." month"; $this->exp_date=date("Y-m-d H:i:s",strtotime($month));

我的模型中有一个afterSave函数,它根据用户给定的开始时间和持续时间保存某些服务的到期日期。我的
afterSave
工作正常,但在保存模型后,它不会被重定向,而是显示一个空白页面

型号:

public function afterSave($insert)
{

    $month= "+".$this->duration_in_months." month";
    $this->exp_date=date("Y-m-d H:i:s",strtotime($month));
    $this->save(['exp_date']);

    return parent::afterSave($insert);
} 
控制器:

if($model->save())

    return $this->redirect(['view', 'id' => $model->sub_id]);

} 

如何重定向afterSave?提前谢谢

正确的方法是从控制器调用
save()
,它将隐式调用
afterSave()

您只需在控制器操作中执行此操作-

if($model->save()){
$this->redirect(……);
}

您可以使用
\Yii::$app->response->redirect('url')->send()
从任何地方进行重定向


您的应用程序显示空白页,因为您在
afterSave()中调用
$this->save(['exp\u date'])
。它再次调用
afterSave()
,并导致无休止的循环。你应该避免这样

尝试
返回$this->redirect()

您的控制器正常,但我发现您的“afterSave”方法有些奇怪。 有

$this->save(['exp_date'])
首先,标准ActiveRecord“save”的第一个参数必须是布尔值。 接下来,您将在这里得到递归——因为“save”方法中调用了“afterSave”方法

所以我想真正的问题是你没有显示任何错误。在包含Yii之前,请尝试在index.php中启用错误报告:

error_reporting(E_ALL);
ini_set('display_errors', '1');
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

这只是为了开发。

我也有同样的问题。但解决办法如下:

public function afterSave($insert, $changedAttributes)
{
    parent::afterSave($insert, $changedAttributes);

    if ($insert) {
        $month = "+".$this->duration_in_months." month";
        $this->exp_date = date("Y-m-d H:i:s", strtotime($month));
        $this->save(false, ['exp_date']);
    }
}

@Kunal..仅以相同的方式实现。。!因此,您已经在控制器中编写了
redirect()
调用,而不是在问题中提到的
Model->afterSave(){…}
中。是否仍显示空白页?如果是,请在此处提及来自控制器的
重定向()调用。更新了问题!您能否在问题中添加调用
save()
的控制器操作代码?缺少返回语句。在开发过程中,您的php.ini应该有错误报告。乱七八糟的代码到处乱放以吐出错误是毫无意义的。