如何从 Yii 中的模型访问控制器函数


How to access a controller function from a model in Yii?

我想从模型内部访问 Yii 的 createUrl() 函数。

这是我在 afterSave() 中的代码。

 public function afterSave(){
    ...more code...
      $message = "Hi ".$this->firstname.' '.$this->lastname.','n
            Welcome to XYZ. This is the mail that is sent for the activation of your account.'n
            Kindly click this link or copy paste it to the URL and register your account.'.$this->createUrl('/user/activate',array('id'=>$this->id,'key'=>$randomKey));
    ...morecode...
    }

这是必须纠正的路线。

$this->createUrl('/user/activate',array('id'=>$this->id,'key'=>$randomKey))

显然,它给了我一个错误。那么如何在模型中使用 createUrl() 函数呢?

问候

你可以改用Yii::app()

Yii::app()->createUrl('/user/activate',array('id'=>$this->id,'key'=>$randomKey))

相关问题: createUrl Yii - 我们应该在控制器视图上调用它,还是无关紧要?

或:

CHtml::link('a_href_label',array('/module_if_exists/controller/action','id'=>$this->id,'key'=>$randomKey),array('target'=>'_blank'));