2012-12-05 8 views
0

컨트롤러 1의 기능 (동작)에서 숨겨진 데이터를 컨트롤러 2의 기능 (동작)으로 보내는 방법을 모르겠습니다.숨겨진 데이터를 Yii의 다른 컨트롤러로 보내는 방법

POST로 두 번째 기능에 데이터를 보내는 것이 좋다고 생각합니다. 양식을 사용하지 않고 POST 데이터 노하우를 보내려고하지 않습니다.

도와 주시겠습니까? 내 영어

Controller1에 대한

죄송합니다 :

클래스 DeviceController이 컨트롤러를 확장 {

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);         

    } 
    } } ?> 

컨트롤러 2 :

클래스 MessagesController exte NDS 컨트롤러 {

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, 
     )); 
    } } 

이 내가 할 것이지만, 그것이 작동하지 않을 것입니다!

+0

몇 가지 코드 예를 들려 줄 수 있습니까? –

+3

무엇을 하시겠습니까? 왜 다른 컨트롤러로 데이터를 보내고 싶습니까? – Sergey

+0

작동하지 않는 기능은 무엇입니까? 컨트롤러 2에서 actionCompose를 호출 할 때 $ data에 아무것도 없다는 것을 알고 있습니까? – ernie

답변

1

마지막으로 내 문제의 해결책을 찾았습니다.

데이터 숨겨진 기능 (action) controller1을 function (action) Controller2로 보내려면 controler1의 기능에서 숨겨진 데이터를 포함하는 세션을 만들고이 세션을 Controller2의 기능으로 가져옵니다. 컨트롤러와 같은 컨트롤러에 renderpartial 백업 된 데이터를 가지고 관련 그들을 처리 호출하여 또한 http://www.larryullman.com/2011/05/03/using-sessions-with-the-yii-framework/

+0

[CController :: forward()] (http://www.yiiframework.com/doc/api/1.1/CController#forward-detail) 함수를 사용해 보았습니까? –

1

오른쪽에서 볼 수있는 한 가지 방법은 메서드를 정적으로 호출하는 것이지만 메서드는 멤버 함수입니다. public static function messageAutoComplete가 적절해야합니다. 또한 컨트롤러에서 호출하는 컨트롤러가 Yii에서 작동하지 않는다는 것을 알았습니다. 컴포넌트 디렉토리 안에 도우미 클래스를 추가해야합니다. $ this-> messageAutoComplete (params)와 같이 호출 할 수 있도록 정적 메서드로 만들지 않으려면 구성 요소를 컨트롤러에서 확장 할 수도 있습니다.

+0

답변 해 주셔서 감사합니다. 하지만 실제로 필요한 것은 폼을 사용하지 않고 POST에서 매개 변수를 전달할 때 controller1을 function (action) 함수에서 Controller2를 함수 (action)라고 부르는 것입니다. 가능합니까? – Florent

0

둘 다보기 파일에 대한 모든 데이터를 보낼 수 있습니다

나는 세션을 만들려면 다음 튜토리얼을 따라 동작.

관련 문제