2014-02-21 9 views
1

한 컨트롤러에서 다른 컨트롤러로 값을 전달하고 싶습니다. 예를 들어 회의 컨트롤러가 있고 새로운 이벤트를 만들고 싶습니다. 컨퍼런스 ID를 이벤트에 전달하여 두 객체가 연관되어 있는지 확인하고자합니다. beforeFilter 메서드를 사용하여 ivar $ 회의에 저장하고 싶습니다. 여기 컨트롤러간에 CakePHP 값 전달

는 이벤트 컨트롤러 내으로, beforeFilter 함수이다 내가 좋아하는 뭔가 URL을 변경할 때마다

public function beforeFilter() { 
    parent::beforeFilter(); 

    echo '1 ' + $this->request->id; 
    echo '2 ' +  $this->request['id']; 
    echo $this->request->params['id']; 
      if(isset( $this->request->params['id'])){ 
      $conference_id = $this->request->params['id'];  
     } 
     else{ 
     echo "Id Doesn't Exist"; 
     } 
} 

:

http://localhost:8888/cake/events/id/3 

또는

http://localhost:8888/cake/events/id:3 

내가 말하는 오류를 얻고있다 id가 정의되지 않았습니다.

어떻게 진행해야합니까? 당신은 당신이 할 수있는 URL을 통해 데이터를 전달하는 때

public $components = array('Session'); 

답변

4

필요 상단에 Conferences 컨트롤러 Events 컨트롤러 물론

$conferenceId = $this->Session->read('conference_id'); 

에서

$this->Session->write('conference_id', $this->request->id); // or the variable that stores the conference ID 

에서

4

, 경유 :

예를 들어
$this->passedArgs['variable_name']; 

당신의 URL 인 경우 :

그런 다음
http://localhost/events/id:7 

당신이 줄

$id = $this->passedArgs['id']; 

당신이 URL을 통해 매개 변수를 받아들이는 컨트롤러 기능에 액세스와 ID, 당신은 사람들을 사용할 수있는 액세스 매개 변수는 다른 변수와 마찬가지로, 예를 들어 URL이 다음과 같이 표시됩니다.

http://localhost/events/getid/7 

컨트롤러 기능은 다음과 같습니다 :

public function getid($id = null){ 
    // $id would take the value of 7 
    // then you can use the $id as you please just like any other variable 
} 
관련 문제