2013-08-17 3 views
0

URL에서 cakephp 컨트롤러로 매개 변수를 전송해야합니다. 메시지 테이블에 두 개의 매개 변수 'ufrom'과 'uto'가 있습니다. 컨트롤러에서이 값을 메시지 테이블에 저장하려고합니다.URL에서 cakephp로 매개 변수를 전달하십시오.

가 나는 URL에 넣어 :

MessagesController에서
http://localhost/ar/messages/add?ufrom=9&uto=3 

내가 가진 기능 :

public function add() { 

if(($this->request->query['uto'])and($this->request->query['ufrom'])){ 
     $this->Message->create(); 
     if ($this->Message->save($this->request->data)) { 
      $this->set('addMessage',TRUE); 
      $this->set('ufrom',$this->request->query['ufrom']); 
      $this->set('uto',$this->request->query['uto']); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The message could not be saved. Please, try again.')); 
     } 

     $targets = $this->Message->Target->find('list'); 
     $this->set(compact('targets')); 
} 
else{ 
    $this->set('error',true); 
} 

}

및 add.ctp에 내가있다 :

<?php 
if(isset($error)){ 
    echo('error'); 
} 
else{ 
    echo json_encode($ufrom); 
    echo json_encode($uto); 
    echo json_encode($addMessage); 
} 
?> 

하지만 위 URL을 사용할 때 나는 본다 :

Notice (8): Undefined variable: ufrom [APP\View\Messages\add.ctp, line 6]null 
Notice (8): Undefined variable: uto [APP\View\Messages\add.ctp, line 7]null 
Notice (8): Undefined variable: addMessage [APP\View\Messages\add.ctp, line 8]null 

그리고 아무 것도 데이터베이스에 저장되지 않는다. 나는 cakephp에서 새로운 사람입니다. 도와주세요. 여기

답변

5

내가 아래

http://www.example.com/tester/retrieve_test/good/1/accepted/active 

같은 PARAMS를 사용하도록 제안 할 수 있지만,이 방법처럼 사용해야 할 경우

http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY 

원하는 값을 얻을 수 아래

echo $this->params['url']['id']; 
echo $this->params['url']['status']; 
귀하의 경우

은 다음과 같을 것입니다

echo $this->params['url']['uto']; 
echo $this->params['url']['ufrom']; 
+0

내 데이터는 아직 데이터베이스에 저장되지 않습니다. – sahar

+0

http://stackoverflow.com/questions/9394080/cakephp-2-0-cannot-save pls this를 참조하십시오. – liyakat

+0

감사합니다. 내 문제가 해결되었습니다. – sahar

0

단순히 때문에 같은 인수로 작업을 통해 전달하는 것입니다 컨트롤러 액션에 매개 변수를 전달하는 가장 쉬운 방법 : 둘째로

http://localhost/ar/messages/add/9/3 

:

public function add($ufrom,$uto) 

귀하의 URL이 같아야합니다 데이터가 URL에서 오는 경우> 요청 -> 데이터를 사용하지 않으려면 다음을 입력하십시오.

$message = array("Message"=>array("ufrom"=>$ufrom,"uto"=>$uto)); 
$this->Message->save($message); 
관련 문제