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

あなたが説明しているのはいわゆるフラッシュメッセージです。ビューに表示して、保存操作が失敗したか成功したかなど、ユーザーに何かを伝えることができます。これらを使用するには、アプリケーションに Session コンポーネントとヘルパーをロードする必要があります。そのため、Controller (またはアプリケーション全体で使用する場合は 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'));
}

単純にエコーして、ビューにフラッシュメッセージを出力してください:

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

それはあなたが探していることをするはずです。

于 2013-08-11T11:13:17.757 に答える