文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Catalyst::Manual::Tutorial::04_BasicCRUD

Catalyst::Manual::Tutorial::04_BasicCRUD

时间:2010-08-20  来源:horsley

Catalyst::Manual::Tutorial::04_BasicCRUD 学习笔记 (2010-08-20 星期五)     文章来源:http://search.cpan.org/~hkclark/Catalyst-Manual-5.8004/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod   padre 升级至: 0.69   一、我们首先尝试使用 url 传递参数   编辑 /MyApp/Controller/Books.pm 文件,添加如下代码:  

sub url_create :Local {
    my ($self, $c, $title, $rating, $author_id) = @_;
    my $book = $c->model('DB::Book')->create({
            title => $title,
            rating => $rating
        });

    $book->add_to_book_authors({author_id => $author_id});
    $c->stash(book => $book,
              template => 'books/create_done.tt2');
}


  编辑 root/src/books/create_done.tt2  文件,内容如下:
 

[% USE Dumper(Indent=1) -%]


[% META title = 'Book Created' %]


<p>

  Added book '[% book.title %]'
  by '[% book.authors.first.last_name %]'
  with a rating of [% book.rating %].

</p>


<p><a href="[% c.uri_for('/books/list') %]">Return to list</a></p>

<pre>
  Dump of the 'book' variable:
  [% Dumper.dump(book) %]
</pre>


USE 语句允许我们引入更多的 TT 插件(不是 Catalyst 插件)。这里我们引入 Data::Dumper。   对于 TT v2.15 (4年前的版本了,应该没人用了吧),代码要做如下变动:

[% authors = book.authors %]
by '[% authors.first.last_name IF authors.first;
       authors.list.first.value.last_name IF ! authors.first %]'


启动服务,能够看到 /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'
INSERT INTO book_author (author_id, book_id) VALUES (?, ?): `4', `6'


点击链接 "Return to list" ,或者刷新浏览器,可以看到现在是6本书了。调试窗口会看到如下信息(N = 1-6):

SELECT author.id, author.first_name, author.last_name 
    FROM book_author me JOIN author author 
    ON author.id = me.author_id WHERE ( me.book_id = ? ): 'N'


二、修改前面的 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:
.-------------------------------------+--------------------------------------.
| Path Spec | Private |
+-------------------------------------+--------------------------------------+
| /books/url_create/*/*/* | /books/url_create |
'-------------------------------------+--------------------------------------'


测试一下,现在我们有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) {
    my ($self, $c) = @_;

    $c->stash(resultset => $c->model('DB::Book'))

    $c->log->debug('*** INSIDE BASE METHOD ***');
}


这里我们将 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:
.-------------------------------------+--------------------------------------.
| Path Spec | Private |
+-------------------------------------+--------------------------------------+
| /books/url_create/*/*/* | /books/base (0) |
| | => /books/url_create |
'-------------------------------------+--------------------------------------'

从以上信息可以看到,访问 /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) {
    my ($self, $c) = @_;
    # Set the TT template to use
    $c->stash(template => 'books/form_create.tt2');
}

为 Form 创建一个模板,编辑 root/src/books/form_create.tt2 :

[% META title = 'Manual Form Book Create' -%]

<form method="post" action="[% c.uri_for('form_create_do') %]">
<table>
  <tr><td>Title:</td><td><input type="text" name="title"></td></tr>
  <tr><td>Rating:</td><td><input type="text" name="rating"></td></tr>
  <tr><td>Author ID:</td><td><input type="text" name="author_id"></td></tr>
</table>
<input type="submit" name="Submit" value="Submit">
</form>

这里指定了 form_create_do方法,接下来我们创建它。

编辑 lib/MyApp/Controller/Books.pm ,内容如下: 

sub form_create_do :Chained('base') :PathPart('form_create_do') :Args(0) {
    my ($self, $c) = @_;

    my $title = $c->request->params->{title} || 'N/A';
    my $rating = $c->request->params->{rating} || 'N/A';
    my $author_id = $c->request->params->{author_id} || '1';

    my $book = $c->model('DB::Book')->create({
            title => $title,
            rating => $rating,
        });

    $book->add_to_book_authors({author_id => $author_id});

    $Data::Dumper::Useperl = 1;

    $c->stash(book => $book,
              template => 'books/create_done.tt2');
}


我们看下服务器的启动信息:

[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec | Private |
+-------------------------------------+--------------------------------------+
| /books/form_create | /books/base (0) |
| | => /books/form_create |
| /books/form_create_do | /books/base (0) |
| | => /books/form_create_do |
| /books/url_create/*/*/* | /books/base (0) |
| | => /books/url_create |
'-------------------------------------+--------------------------------------'


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 功能

相关阅读 更多 +
排行榜 更多 +
泡龙大闯关安卓版

泡龙大闯关安卓版

冒险解谜 下载
割草派对安卓版

割草派对安卓版

飞行射击 下载
堡垒攻防战安卓版

堡垒攻防战安卓版

飞行射击 下载