How to access controller function from model in Yii?

I want to access the createUrl () function of Yii from inside the 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 needs to be fixed.

$ this-> createUrl ('/ user / Activate', array ('ID' => $ this-> ID, 'key' => $ use random))

Obviously this gives me an error. So how can I use the createUrl () function from the model?

Hi,

+4
source share
2 answers

Instead, you can use Yii::app() :

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

Related question: createUrl Yii - should we call it as a controller or does it not matter?

+4
source

or

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

+2
source

Source: https://habr.com/ru/post/1501693/


All Articles