Question

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!

Était-ce utile?

La 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/

Autres conseils

One thing that I see right off hand is you are calling your method statically but the method is a member function. Should be public static function messageAutoComplete to be proper. Also I have noticed that calling controllers from a controller doesn't work in Yii. You should really be adding that is a helper class inside your components directory. You can even extend your controller from you component if you don't want to make it a static method so it would be called like $this->messageAutoComplete(params)

Also you can send all the data to both view files by calling renderpartial in on controller and in same controller take the backed data and process them in the related action.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top