■
symfonyのJavascriptヘルパーで利用できる小技を1つ。
symfonyはPrototypeとscript.aculo.usをJavascriptを1行も書かずにsymfonyのヘルパー機能で呼び出すことができます。
本家ドキュメント:http://www.symfony-project.com/book/trunk/11-Ajax-Integration
その中のヘルパー関数の1つにlink_to_remote()というのがあります。
- link_to_remote()の利用例
<?php echo link_to_remote('Delete this post', array( 'update' => 'feedback', 'url' => 'post/delete?id='.$post->getId(), '404' => "Element.show('404result')", )) ?>
その名のとおり、AJAXでサーバーからレスポンスを受けるのですが、本家ドキュメントにはステータスで判断できる属性として
success: When the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range
failure: When the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range
404: When the request returns a 404 status
の3つが書かれています。
通常はこれで十分ですが、認証処理が必要なページでセッションがタイムアウトした場合などは401で受けたいですよね。
その場合は404のようにステータスコードを指定すれば、そのコードが返ってきたときに処理をさせることができます。
参照: http://www.symfony-project.com/snippets/snippet/106
次のようにステータスコードが401用の処理を追加します。
<?php echo link_to_remote('Delete this post', array( 'update' => 'feedback', 'url' => 'post/delete?id='.$post->getId(), '404' => "Element.show('404result')", '401' => "if ( confirm('再度ログインしてください')) {document.location='/';}", )) ?>
401の場合は、"再度ログインしてください"というポップアップ表示と、ドキュメントルートにロケーションさせています。
と、ここで401が指定できるならその他もできるのでは?と思いやってみたらできました。
というわけで、200だけ受けて動作させるとか絞り込んだ動作を指定できますね。