Search code examples
yii-extensionsyiiyii-componentsyii-events

How send hidden data to another controller action on Yii


I do not know how to send hidden data from a function (action) of controller1 to a function (action) of Controller2 in yii.

I think sending the data to the second function by POST, I do not aim to send POST data know-how without using a form.

Can you help me?

Sorry for my english

Controller1 :

class DeviceController extends Controller {

 public function actionDeviceTurnOn(){

     if(isset($_GET['id_device'])){

         $id_device = $_GET['id_device'];

         $model = $this->loadModel($id_device);
         $model->status = 1;

         $title = "Message of admin";

         $message = "Good morning" . "\r\n" .
                     "\r\n" . 
                     "The device is On";


         MessagesController::messageAutoComplete(Yii::app()->user->id, 
           $_GET['id_user'], $title, $message);                                  

     } 
  }  } ?>

Controller 2 :

class MessagesController extends Controller {

  public function messageAutoComplete($from_user_id=null, $to_user_id=null,
  $title=null, $message=null){

        $data['from_user_id'] = $from_user_id;
        $data['to_user_id'] = $to_user_id;
        $data['title'] = $title;
        $data['message'] = $message;

        MessagesController::actionCompose($data);        
  }


  public function actionCompose ($data=null)
  {
        $model=new Messages;

        $this->performAjaxValidation($model);

        if(isset($_POST['Messages']))
        {
          foreach($_POST['Messages']['to_user_id'] as $user_id) {
              $model = new Messages;
              $model->attributes=$_POST['Messages'];
              $model->to_user_id = $user_id;
              $model->save();
          }
              $this->redirect(array('success'));
        }

        $model->to_user_id = "";

        if($data != null){

             $model->from_user_id = $data['from_user_id'];
             $model->to_user_id = $data['to_user_id'];
             $model->title = $data['title'];
             $model->message = $data['message'];
        }

        $this->render('compose',array(
           'model'=>$model,
        ));
   }  }

This is what I would do but it does not work!


Solution

  • Finally, I found the solution to my problem.

    To send data hidden function (action) controller1 to a function (action) Controller2, I created a session that contains the hidden data in the function of controler1 and I get this session in the function of Controller2.

    I followed the following tutorial to create session: http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/