@kyanny's blog

My thoughts, my life. Views/opinions are my own.

今日のCPAN

CGI::Application::Plugin::Forwardが1.05にバージョンアップ

Changesを読んでみると、Redirectとの差を説明するドキュメントを追加したとのこと。

1.05 October 03, 2005
- fixed to work with old versions of CGI::Application that don't
support callbacks

- now explicitly 'use CGI::Application' to make the registration
of hooks it possible to register hooks at class load time.

- added docs describing the difference between this module and
CGI::Application::Plugin::Redirect, and also providng some
notes on when to use each method.

で、その部分を読んでみる。ちゃんとFORWARD VS REDIRECTという索引も増えている。

Calling forward changes the run mode of your application, but it stays within the same HTTP request.

To redirect to a new runmode using a completely new web request, you might consider using the redirect method provided by CGI::Application::Plugin::Redirect.

The advantage of using an external redirect as opposed to an internal forward is that it provides a 'clean break' between pages.

For instance, in a typical BREAD application (Browse, Read, Edit, Add, Delete), after the user completes an action, you usually return the user to the Browse list. For instance, when the user adds a new record via a POST form, and your app returns them to the list of records.

If you use forward, then you are still in the same request as the original add record. The user might hit reload, expecting to refresh the list of records. But in fact, reload will attempt to repost the add record form. The user's browser might present a warning about reposting the same data. The browser may refuse to redisplay the page, due for caching reasons.

So in this case, it may make more sense to do a fresh HTTP redirect back to the Browse list.

15分程度で上の文を訳してみた。


run_mode を変えるために forward メソッドを呼ぶと、同じ HTTP リクエストになります。

完全に新しいリクエストとして別の run_mode に移行したいときは、CGI::Application::Plugin::Redirect モジュールの redirect メソッドを使うといいでしょう。

内部的な forward よりも外部への redirect のほうが良い点は、ページ間の ’完全な破壊’をもたらすことです。

閲覧、データの編集が行える典型的なウェブアプリケーション(BREADと呼ばれる)で、ユーザの操作後に常にリストを返すとします。ユーザが POST メソッドで新しいレコードを追加したのち、レコードのリストを返します。

forward を使うと、追加したときと同じ HTTP リクエストを維持したままです。ユーザがブラウザをリロードすると、リストが更新されますが、実際には、ブラウザはリロードすると同じ HTTP リクエスト(この場合、POST メソッドでレコードを追加したリクエスト)を再送信しようとします。ブラウザは同じデータを再送信しようとしている警告のダイアログを表示するでしょう。キャッシュのため、ブラウザは画面の再表示をしないでしょう。

そのような場合には、新しい HTTP リクエストとしてブラウザにリストを返すようにしたほうがいいでしょう。

ということで、ケースバイケースでうまく使い分けてね、というのが作者のMichael Grahamさんの意見なんだろう。Redirectモジュールの作者は別みたいだけど、この名前はCGI::Applicationのメーリングリストで見かけたことがある。積極的にコミットしている人なんだなあと思っていたら、Redirectのほうの作者のCees Hekさんも昨日メーリングリストで返事をしていた人だった。うーん、CPAN Authorがごろごろいるってすごいなあ。