将参数从url传递给cakephp

sah*_*har 0 cakephp

我需要从URL发送参数到cakephp控制器.我有两个参数'ufrom'和'uto'的消息表.在控制器中我想将此值保存在消息表中.

我输入了网址:

http://localhost/ar/messages/add?ufrom=9&uto=3
Run Code Online (Sandbox Code Playgroud)

在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);
}
Run Code Online (Sandbox Code Playgroud)

}

在add.ctp我有:

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

但是当我使用上面的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
Run Code Online (Sandbox Code Playgroud)

并且没有任何内容存储在数据库中.我是cakephp的新手.请帮忙.

liy*_*kat 5

在这里我可以建议你使用下面的参数

http://www.example.com/tester/retrieve_test/good/1/accepted/active
Run Code Online (Sandbox Code Playgroud)

但如果你需要这样使用

http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
Run Code Online (Sandbox Code Playgroud)

你可以得到如下的价值

echo $this->params['url']['id'];
echo $this->params['url']['status'];
Run Code Online (Sandbox Code Playgroud)

在你的情况下会是这样的

echo $this->params['url']['uto'];
echo $this->params['url']['ufrom'];
Run Code Online (Sandbox Code Playgroud)