redirect from my model

Hello,

In my model I use logic, if data not found then redirect to different page. I use this redirection code,

$this->redirect(array(‘author’));

Important note is that, I have two modules, and author comes from "abc" modules.

but redirection is not working. any idea?

CException

Description

Address does not have a method named "redirect".


Yii::app()->getController()->redirect('url');

Now I can redirect but I want to pass the value to the new page

Yii::app()->getController()->redirect("http://abc…com/abc/index.php?r=def/default/auther?systems=$this->systems");

I got this

http://abc…com/abc/index.php?r=def/default/auther?systems=Array

why I got array, I need the value there. Any idea

how could i know ? you defined the value of the variable you are trying to pass

the following is better




Yii::app()->getController()->redirect(array('def/default/auther',array('systems'=>$this->systems)));

anyway, even with this code the value will still be an array

wow, great working. :rolleyes:

Thanks

You shouldn’t be doing a redirect in your model

where then is the best place to do a redirect?

You should really only redirect in the controller.

The model should handle your business rules. Some day you might want to use it in a non-web context, such as a console command, and the redirect would make no sense in that scenario.

The view should ideally do no more than display data formatted appropriately.

You can throw an exception in you model, and in controller in "try catch" implement logic of redirection

the actual redirect logic should be in the control, as noticed above. Just to help.




$model = '/Get/your/data';

if (is_null($Model)) {

   $this->redirect();

} else {

   //Do whatever

}