Perl日記

日々の知ったことのメモなどです。Perlは最近やってないです。

MVCその9

続き。
というわけで、今までやってきたけれど、CatalystでなんとかMVCの三つを形にした。
付属のサーバで起動してみる。

$ script/myhatena_web_server.pl

以下、ターミナルに出力される情報など。


読み込んだモジュール。

[debug] Loaded components:
.-----------------------------------------------------------------+----------.
| Class                                                           | Type     |
+-----------------------------------------------------------------+----------+
| MyHatena::Web::Controller::Root                                 | instance |
| MyHatena::Web::Controller::Search                               | instance |
| MyHatena::Web::Model::Tag::Result                               | instance |
| MyHatena::Web::Model::Tag::Search                               | instance |
| MyHatena::Web::View::TT                                         | instance |
'-----------------------------------------------------------------+----------'

アクション。

[debug] Loaded Private actions:
.----------------------+--------------------------------------+--------------.
| Private              | Class                                | Method       |
+----------------------+--------------------------------------+--------------+
| /search              | MyHatena::Web::Controller::Search    | search       |
| /default             | MyHatena::Web::Controller::Root      | default      |
| /end                 | MyHatena::Web::Controller::Root      | end          |
| /index               | MyHatena::Web::Controller::Root      | index        |
'----------------------+--------------------------------------+--------------'

ディスパッチパス。

[debug] Loaded Path actions:
.-------------------------------------+--------------------------------------.
| Path                                | Private                              |
+-------------------------------------+--------------------------------------+
| /                                   | /index                               |
| /                                   | /default                             |
| /search                             | /search                              |
'-------------------------------------+--------------------------------------'

http://localhost:3000/ にアクセス。

[debug] "GET" request for "/" from "127.0.0.1"
[debug] Path is "/"
[debug] Rendering template "index.tt"
[info] Request took 0.096767s (10.334/s)
.------------------------------------------------------------+-----------.
| Action                                                     | Time      |
+------------------------------------------------------------+-----------+
| /index                                                     | 0.000233s |
| /end                                                       | 0.089922s |
|  -> MyHatena::Web::View::TT->process                       | 0.088519s |
'------------------------------------------------------------+-----------'

TTのprocessメソッドがちゃんと走ってる。
んでそのテンプレートはindex.ttが使われている。


ここで、http://localhost:3000/index にアクセスしてみると、

[info] *** Request 2 (0.005/s) [3406] [Fri Feb 26 21:56:48 2010] ***
[debug] "GET" request for "index" from "127.0.0.1"
[debug] Path is "/"
[debug] Arguments are "index"
[info] Request took 0.004630s (215.983/s)
.------------------------------------------------------------+-----------.
| Action                                                     | Time      |
+------------------------------------------------------------+-----------+
| /default                                                   | 0.000238s |
| /end                                                       | 0.000389s |
'------------------------------------------------------------+-----------'

defaultアクションにディスパッチされて、404エラーとなる。
なぜならindexアクションはあくまでルート直下に来たリクエストしか対応しないようになっているから。

lib/MyHatena/Web/Controller/Root.pm
sub index :Path :Args(0) {

Pathの定義を空にしておくと、名前空間のルートにマッチします。
404 Not Found

Root.pmにおいて、「:Path」は 「:Path('/')」と同義である。
ゆえに index()は、「http://localhost:3000/」にディスパッチされるのであって、「http://localhost:3000/index」にはなりえないということだな。


検索語句に「Perl」と入れて、はてブのタグ「Perl」の新着一覧を取得してみる。

[info] *** Request 3 (0.042/s) [4198] [Fri Feb 26 22:20:52 2010] ***
[debug] Query Parameters are:
.-------------------------------------+--------------------------------------.
| Parameter                           | Value                                |
+-------------------------------------+--------------------------------------+
| search_tag                          | Perl                                 |
'-------------------------------------+--------------------------------------'
[debug] "GET" request for "search" from "127.0.0.1"
[debug] Path is "search"
[debug] Rendering template "search.tt"
[info] Request took 0.306377s (3.264/s)
.------------------------------------------------------------+-----------.
| Action                                                     | Time      |
+------------------------------------------------------------+-----------+
| /search                                                    | 0.222587s |
| /end                                                       | 0.006618s |
|  -> MyHatena::Web::View::TT->process                       | 0.005301s |
'------------------------------------------------------------+-----------'

出た。
テンプレートは、search.ttが使われている。
namespace => '' にしたから、search/seach.ttではない。
そういえば namespace => '' の意味はまだよく分かっていなかった。


ああ、いま気がついたけれど、Controllerのテストを書いてなかったな…。
まあ事後承諾というか後付論理というか退行試験対策というか。
あとにする。

とりあえずは動いたことの喜びを感じたい。
以上。