Pass data from one action to another action

Generally we can go to one action to another action by using




$this->redirect('.....');



But if we need to pass some value or object from one action to another action then how it can be done? coz there is no option for passing any object in redirect.

There is session but use of frequent sessions can put some extra load on the server. What are the possible alternatives for this in Yii?

try this





public function actionXX(){


     $_GET['someKey'] = $anyVal ;

     $this->forward('someRoute',false);

     unset($_GET['someKey']);


}




may can use the $_GET/$_POST… global variable to pass params , or some another global shared component to pass param (Yii::app()->params[‘someKey’] ? ) . just my thought hope you find a better solution . :lol:

Lets say the action being performed is the update action, and you want to redirect to the "view" action, passing along the id of the model just updated. Then do this:




$this->redirect(array('view', 'id' => $model->id));



Also, (regarding Maclein’s response) avoid directly referring to $_GET and $_POST. Yii frees you from that, by allowing you declare variables in the function declaration for any action. If you still need to refer to the get or the post, do it safely, by defining a default value in the event the variable is missing from the get/post:




// GET

$foo = Yii::app()->request->getQuery('foo', 0);


// Post

$foo = Yii::app()->request->getPost('foo', 0);


// GET *OR* POST

$foo = Yii::app()->request->getParam('foo', 'defaultValue');



:mellow:

Thanks for the reply… it is really good… and sounds descent as a solution. Thanks.

@Emily One more thing if you can suggest, what are the possible ways to maintain(or keep alive) chain life cycle of request objects in Yii?? Please tell me with some example.

@Maclein - Can you define “chain life cycle of request objects”? :mellow:

Yes sure. The scenario is, I am posting a form and its action is defined to




public function actionSomething(){

}



Now this action method has a filter method too. Now when inside actionSomething() I am trying to get $_POST[‘x’]

its saying undefined index.

So how to get the $_POST value inside actionSomething() ? Here filter is necessary. I have to keep that.

Please give me some suggestion.




public function actionSomething($x) {

// If $x was either on the POST or the GET, it's value will be set, so you can reference $x

// If $x was not on the POST or the GET, you'll get a run-time error.

}



Did that answer your question?

:mellow:

No actually that was my fault totally… I was doing something wrong over there and thinking that filter is failing to keep alive the requested objects. I put there some checking and got it was not coming in either cases. Its a silly mistake. Now that’s fine… :P

@Emily One more thing is there any reference so that i can use json in yii. any guide?

Plenty of references, just google "yii jquery". E.g.

http://usingjquery.com/2010/10/using-jquery-with-the-yii-framework/

:mellow:

Thanks for this reference. I will have to take a deep look at it. Thanks. :)

@Emily What I was trying to do is that I wanted to make clean the url. But it was giving so much errors and rest of the pages are not working. What I have done is that




'urlManager'=>array(

    'urlFormat'=>'path',

    //rules 

    ),



in main.php

but after doing that




<img src="images/base/logo.gif" width="307" height="65" border="0">



this image is not working

Similarly




/index.php?r=controller/action 



is not working.

Please help…!!! :unsure: