Chinaunix首页 | 论坛 | 博客
  • 博客访问: 589856
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类:

2009-03-29 08:38:08

CGI::Application 是个轻量级的web框架,呃。。。其实只是个基本基本的框架 :)。不过,它有好多的扩展,利用这么扩展模块,我们自己来实现一个 MVC 的 web 框架也不是难事。
 
先看看咱们这个框架的目录结构:
 
MyWeb/ -  start.pl
       |- lib/  - MyWeb.pm
       |        |- MyWeb/ - Dispatch.pm
       |                  - Search.pm
       |                    .....
       |- config/
       |- db/
       |- template/
       |- tools/
 
 
MyWeb.pm
 

package MyWeb;

use strict;
use warnings;

use base 'CGI::Application';

use CGI::Application::Plugin::Config::Simple;
use CGI::Application::Plugin::AutoRunmode;
use CGI::Application::Plugin::AnyTemplate;
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::ValidateRm;

use Class::DBI::Loader;
use Class::DBI::Pageset;

sub cgiapp_init {
    my $self = shift;

    $self->header_add( -charset => 'GBK');
    
    $self->config_file('./config/site.ini');
    $self->{'__cdbi__loader__'} = Class::DBI::Loader->new(
        dsn            => $self->config_param('db.dsn'),
        user        => $self->config_param('db.user'),
        passsword    => $self->config_param('db.password'),
        namespace    => 'MyWeb',
        additional_classes    => qw/Class::DBI::Pageset/,
    );
    
    $self->template->config(
        default_type                => 'TemplateToolkit',
        include_paths                => './template',
        auto_add_template_extension => 0,
    );
    
    $self->session_config();
}

sub teardown {
    my $self = shift;
}
    
1;

有了 AutoRunmode 和 Dispatch, start.pl 就很简单了

#!/usr/bin/perl

use strict;
use warnings;

use lib './lib';
use MyWeb::Dispatch;

MyWeb::Dispatch->dispatch();

下面看看 Dispatch 的示例

package MyWeb::Dispatch;

use strict;
use warnings;

use base 'CGI::Application::Dispatch';

sub dispatch_args {
    return {
        args_to_new => { TMPL_PATH => './template/', },
        prefix => 'MyWeb',
        table => [
            '' => { app => 'View', },
            ':app' => {},
            ':app/:rm' => {},
            ':app/:rm/:id' => {},
        ],
    };
}

1

再给个 control 的示例 View.pm

package MyWeb::View;

use strict;
use warnings;

use base 'MyWeb';

#
# Runmodes -----------------------
#
sub default : StartRunmode {
    my $self = shift;

    $self->template->fill('default.tt');        
}
    
sub node : Runmode {
    my $self = shift;
    
}

sub error : ErrorRunmode {}
    
1;

Search.pm

package MyWeb::Search;

use strict;
use warnings;

use base 'MyWeb';
use URI::Escape;

#
# Runmodes -----------------------
#

sub default : StartRunmode {
    my $self = shift;
    
    my %tables = (
        '中药' => 'zhongyao',
        '方剂' => 'fj_info_gb2312',
        '西药' => 'xiyao',
        '辞典' => 'fangji',
    );
    my $q        = $self->query->param('q'); # if $q is null, it should be redirected to an error page...

    my $meta    = $self->query->param('meta');     
    my $query = '%' . $q . '%';
    
    my $dict    = $self->{'__cdbi__loader__'}->find_class($tables{"$meta"});

    my $pager = $dict->pager({
        entries_per_page => 20,
        current_page => $self->query->param('page') || 1,
        pages_per_set => 5,
    });
 
    my $results = $pager->search_like(mingcheng => $query);
    
    $self->template->fill('search.tt',
        { results => $results,
         pager => $pager,
         q => uri_escape($q),
         meta => uri_escape($meta),
         lib => uri_escape($meta),}
    );        

}

=head    
sub node : Runmode {
    my $self = shift;
    
}
=cut

sub error : ErrorRunmode {}

1;

在 Search.pm 这个例子中,因为我们用了 Class::DBI::Loader Class::DBI::Pageset,所以可以很轻松的查询并实现分页显示,我们都不用写SQL代码!

 

阅读(779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~