MojoliciousLite: 实时的web框架 概述

MojoliciousLite:  实时的web框架:

SYNOPSIS 简介:

 #  Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
    use Mojolicious::Lite;
  
       # Route with placeholder
        get '/api/:foo' => sub {
        my $c   = shift;
        my $foo = $c->param('foo');
        $c->render(text => "Hello from $foo.");
               };
  
                    # Start the Mojolicious command system
        app->start;


zjzc01:/root/mojo# curl http://127.0.0.1:3000/api/xx
Hello from xx.

zjzc01:/root/mojo# curl http://127.0.0.1:3000/api/abc123
Hello from abc123.zjzc01:/root/mojo# 

描述:

Mojolicious::Lite 是一个微型实时框架:

Mojolicious::Lite  实现了下面的函数,自动导出。


any

my $route = any '/:foo' => sub {...};
  my $route = any '/:foo' => sub {...} => 'name';
  my $route = any '/:foo' => {foo => 'bar'} => sub {...};
  my $route = any '/:foo' => [foo => qr/\w+/] => sub {...};
  my $route = any ['GET', 'POST'] => '/:foo' => sub {...};
  my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub {...};


声明 route 为any 在Mojolicious::Routes::Route, 匹配所有的HTTP 请求方法或者all.

app

my $app=app;

返回Mojolicious::Lite 应用对象

  # Use all the available attributes and methods
  app->log->level('error');
  app->defaults(foo => 'bar');


del

  my $route = del '/:foo' => sub {...};
  my $route = del '/:foo' => sub {...} => 'name';
  my $route = del '/:foo' => {foo => 'bar'} => sub {...};
  my $route = del '/:foo' => [foo => qr/\w+/] => sub {...};


匹配DELETE 请求

get

  my $route = get '/:foo' => sub {...};
  my $route = get '/:foo' => sub {...} => 'name';
  my $route = get '/:foo' => {foo => 'bar'} => sub {...};
  my $route = get '/:foo' => [foo => qr/\w+/] => sub {...};


只匹配get请求


group 

  group {...}

开始一个route group



Routes:

Routes 只是对请求过来的路径指向不同的函数,可以在路径中包含不同的占位符,

传进来的一个参数是$c  是 Mojolicious::Controller 对象本身,它也包含着HTTP request和HTTP reponse的对象:


GET/POST 传进来的参数:

use Mojolicious::Lite;

# /foo?user=sri
get '/foo' => sub {
  my $c = shift;
  my $user = $c->param('user');
  $c->render(text => "Hello $user.");
};

app->start;


















posted @ 2016-03-25 15:54  czcb  阅读(139)  评论(0编辑  收藏  举报