The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Book::Chinese::MasterPerlToday::Catalyst - Catalyst Framework

DESCRIPTION

本章主要介绍如何使用 Catalyst 来构建一个程序。

    cpan> install Catalyst::Runtime
    cpan> install Catalyst::Devel
    cpan> install Catalyst::Manual

详尽的文档请参阅 Catalyst::Manual

前言

所有模块或工具的用意都在于简化您任务所需的代码。

Catalyst 采用当前最流行的 MVC 结构。

  • V(View)

    MVC 中的 V 是比较清晰的,输出可以是直接的文本($c->res->body),可以是 HTML(如 Catalyst::View::TT), 可以是 RSS, 或者是 JSON 或其他的(如 Wx 等 GUI)。所有的输出都是经过 ->res->body 或 ->res->write, 最终由 Catalyst::Engine::* 来渲染。

  • M(Model)

    Catalyst 中的 M 是瘦 Model. 常见的通过 Catalyst::Model::DBIC::SchemaCatalyst::Model::Adaptor 来加载外部 Model. 使用这种方法的好处是:真正的 DBIx::Class 结构 ORM 或其他模块可以作为 Catalyst Model,也可以被 cron 等外部 pl 脚本使用。

  • C(Controller)

    Controller 与 Model 是最容易混淆的结构。有些代码你不知道放 Model 里还是放 Controller 里好。个人浅见,以该代码是否可以被外部 pl 程序调用来决定放哪里。

    Controller 最大的用处是定义一系列的 URI 和通过 params 或 Model 的返回值来确定使用采用什么 action。简而言之,连接 Model 和 View 的一个桥梁。

FAQ

  • auto 中怎么用 $c->detach 不好使?

        if ( $bla ) {
            $c->forward('/where/some');
            return 0;
        }
        return 1;

    在 sub auto 中,return 值是非常重要的。return 0 就直接去 sub end 了。

  • param 和 params

        use Data::Dumper;
        sub test_params :Path('test_params') {
            my ($self, $c) = @_;
           
            my $test = {
                a => $c->req->param('a'),
                b => $c->req->param('b'),
                c => $c->req->param('c'),
            };
            $c->res->body(Dumper(\$test));
        }

    当 DBIx::Class ->create 的时候,上文这种格式还是比较常见的。但是这种格式返回的结果可能会让你大吃一惊。

        E:\Fayland\chinese-perl-book\eg\Catalyst\TestApp>perl script/testapp_test.pl /test_params?b=1
        # 省略输出
        $VAR1 = \{
                '1' => 'c',
                'a' => 'b'
              };

    快速的修正可以使用 params

        sub test_params2 :Path('test_params2') {
            my ($self, $c) = @_;
           
            my $test = {
                 a => $c->req->params->{'a'},
                 b => $c->req->params->{'b'},
                 c => $c->req->params->{'c'},
            };
            $c->res->body(Dumper(\$test));
        }
    
        E:\Fayland\chinese-perl-book\eg\Catalyst\TestApp>perl script/testapp_test.pl /test_params2?b=1
        # 省略输出
        $VAR1 = \{
                'c' => undef,
                'a' => undef,
                'b' => '1'
              };

资源

AUTHOR

Fayland Lam, <fayland at gmail.com>

COPYRIGHT & LICENSE

Copyright (c) 2009 Fayland Lam

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.