0

我认为这是一件非常容易的事情。假设,我正在查看控制器的相应功能。函数看起来像:

class SomeController extends AppController{
public function action(){
.
.
.
if($this->request->is('post')){
   .
   .
   .
   if(some error appears)
      I want to get back to the "action" view with the data that was given to it before
   else
      continue processing
   .
   .
   .
}
populate $data and send it to the view "action";
}

我的意思是,我只想随时返回到带有数据的特定视图。我用过redirect(array('controller'=>'some','action'=>'action')),但没有用。并且 usingrender()不会占用 $data。请帮我。

4

1 回答 1

2

您所描述的是所谓的flash messages,它们可以显示在您的视图中以告诉用户一些事情,例如保存操作失败或成功。您需要在应用程序中加载 Session 组件和帮助程序才能使用它们。因此,在您的控制器(或 AppController,如果您想在应用程序范围内使用它)中,添加:

public $components = array('Session');
public $helpers = array('Session');

然后在您的控制器中设置所需的闪存消息:

if (!$this->Model->save($this->request->data)) {
    // The save failed, inform the user, stay on this action (keeping the data)
    $this->Session->setFlash('The save operation failed!');
} else {
    // The save succeeded, redirect the user back to the index action
    $this->redirect(array('action' => 'index'));
}

通过简单地回显,确保在您的视图中输出 Flash 消息:

echo $this->Session->flash();

那应该做你正在寻找的东西。

于 2013-08-11T11:13:17.757 回答