我需要将参数从 URL 发送到 cakephp 控制器。我有带有两个参数“ufrom”和“uto”的消息表。在控制器中,我想将此值保存在消息表中。
我输入网址:
http://localhost/ar/messages/add?ufrom=9&uto=3
在 MessagesController 我有功能:
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 的新手。请帮忙。