简体   繁体   中英

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

I want to access the createUrl() function of Yii from inside a model.

This is my code in my 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 is the line that must be rectified.

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

Obviously, it is giving me an error. So how do I use the createUrl() function from within the model?

Regards,

You can use Yii::app() instead:

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

Related question: createUrl Yii - should we call it on controller view, or doesn't matter?

要么:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM