0

私の symfony2 アプリでは、ユーザーがいくつかのファイルをダウンロードできるようにしています:

class FileController extends Controller
{
     ...

     public function downloadFilesAction()
     {
        //This will serve a page listing all downloadable files and their url

     }

     public function downloadFileAction($file_id)
     {
         //This will be requested from the page served 
         //in the above "downloadFilesAction()"

         try {
             // Some processing goes here ...
         }
         catch (\Exception $e) 
         {
             $this->get('session')->getFlashBag()->add('error', 'Error occurred');
             return $this->forward('MyBundle:File:downloadFiles');
         }

         //get file content into &content variable

         $headers = array(
             'Content-Type' => 'text/csv;',
             'Content-Disposition' => "attachment; filename*=UTF-8''downloadedfile.csv",
         ); 

         return new Response($content, 200, $headers);
     }
}

初めてエラーが発生し、フラッシュ エラー メッセージが表示されたとします。2回目のダウンロードは成功しました。問題は、2 番目のリクエスト フラッシュ メッセージがまだページに残っていることです。ダウンロードが成功したときのフラッシュメッセージを取り除くにはどうすればよいですか?

4

2 に答える 2

0

get('error')最初の内部を使用して、フラッシュバッグの既存のメッセージをクリアするだけdownloadFileAction()です。

FlashBag->clear()を使用すると、すべてのメッセージの設定が解除されることに注意してください (これは必要ありません)。

public function downloadFileAction($file_id)
{
    $flashBag = $this->get('session')->getFlashBag();
    $flashBag->get('error'); // clear the error message

    try {
         // ...
    }
    catch (\Exception $e) 
    {
        $flashBag->add('error', 'Error occurred');

        return $this->forward('MyBundle:File:downloadFiles');
    }

    // ...
于 2014-03-19T10:10:31.440 に答える
0

フラッシュバッグをリセットするには、アクションを転送する代わりにユーザーをリダイレクトできます:

return $this->redirect($this->generateUrl('download_files_route'));
于 2014-03-19T15:17:03.443 に答える