Catalyst::Manual::Tutorial::04_BasicCRUD
时间:2010-08-20 来源:horsley
sub url_create :Local { |
编辑 root/src/books/create_done.tt2 文件,内容如下:
[% USE Dumper(Indent=1) -%]
Added book '[% book.title %]' </p>
<pre> |
USE 语句允许我们引入更多的 TT 插件(不是 Catalyst 插件)。这里我们引入 Data::Dumper。 对于 TT v2.15 (4年前的版本了,应该没人用了吧),代码要做如下变动:
[% authors = book.authors %] |
启动服务,能够看到 /books/url_create :
$ DBIC_TRACE=1 script/myapp_server.pl -r |
我们测试一下,输入:
http://localhost:3000/books/url_create/TCPIP_Illustrated_Vol-2/5/4 |
浏览器会显示信息:
Added book 'TCPIP_Illustrated_Vol-2' by 'Stevens' with a rating of 5." |
服务端,可以看到如下 DBIC 的调试信息:
INSERT INTO book (rating, title) VALUES (?, ?): `5', `TCPIP_Illustrated_Vol-2' |
点击链接 "Return to list" ,或者刷新浏览器,可以看到现在是6本书了。调试窗口会看到如下信息(N = 1-6):
SELECT author.id, author.first_name, author.last_name |
二、修改前面的 action 为 chained 属性
编辑 lib/MyApp/Controller/Books.pm ,内容如下:
sub url_create :Chained('/') :PathPart('books/url_create') :Args(3) {
my ($self, $c, $title, $rating, $author_id) = @_; |
Chained 用于精确控制参数以及 controller 任务分发,它包含三个部分:
- Beginning
- Use ":Chained('/')" to start a chain
- Get arguments through CaptureArgs()
- Specify the path to match with PathPart()
- Middle
- Link to previous part of the chain with :Chained('_name_')
- Get arguments through CaptureArgs()
- Specify the path to match with PathPart()
- End
- Link to previous part of the chain with :Chained('_name_')
- Do NOT get arguments through "CaptureArgs()," use "Args()" instead to end a chain
- Specify the path to match with PathPart()
我们的程序中,Chained('/') 用于开始一个 chain, :PathPart('books/url_create') 用于指定匹配的 URL,:Args(3)用户捕获三个参数并结束 chian。
参考资料: "Action types" in Catalyst::Manual::Intro, Catalyst::DispatchType::Chained,http://www.catalystframework.org/calendar/2006/10.
启动服务,你会发现 /books/url_create 信息变成 Chianed actions,url 中也可以看到它捕获三个参数:
[debug] Loaded Chained actions: |
测试一下,现在我们有7本书了:
http://localhost:3000/books/url_create/TCPIP_Illustrated_Vol-2/5/4 |
我们继续改进,编辑 lib/MyApp/Controller/Books.pm ,添加方法:
sub base :Chained('/') :PathPart('books') :CaptureArgs(0) { |
这里我们将 resultset 存入 stash,因此 base 行为链条(chain) 上的后续动作都可以由此获益,比如我们通过 base 获取一个 book id ,然后使用 ->find(id)存入 stash。
我们试一下修改 url_create ,编辑 lib/MyApp/Controller/Books.pm ,修改如下:
sub url_create :Chained('base') :PathPart('url_create') :Args(3) { |
启动服务,我们会发现如下信息:
[debug] Loaded Chained actions: |
从以上信息可以看到,访问 /bootks/url_create 时触发了 base ,因此 url_create 的第一个参数可以忽略,但为了明白起见,我们还是保留。
看下效果吧,浏览器打开:
http://localhost:3000/books/url_create/TCPIP_Illustrated_Vol-2/5/4 |
三、手工创建一个 Create Form
首先,添加一个方法用于显示 Form,编辑 lib/MyApp/Controller/Books.pm :
sub form_create :Chained('base') :PathPart('form_create') :Args(0) { |
为 Form 创建一个模板,编辑 root/src/books/form_create.tt2 :
[% META title = 'Manual Form Book Create' -%] |
这里指定了 form_create_do方法,接下来我们创建它。
编辑 lib/MyApp/Controller/Books.pm ,内容如下:
sub form_create_do :Chained('base') :PathPart('form_create_do') :Args(0) { |
我们看下服务器的启动信息:
[debug] Loaded Chained actions: |
Point your browser to http://localhost:3000/books/form_create and enter "TCP/IP Illustrated, Vol 3" for the title, a rating of 5, and an author ID of 4. You should then see the output of the same create_done.tt2 template seen in earlier examples. Finally, click "Return to list" to view the full list of books.
Note: Having the user enter the primary key ID for the author is obviously crude; we will address this concern with a drop-down list and add validation to our forms in Chapter 9.
四、添加一个简单的 delete 功能