您的位置:首页 > 其它

DNS安装及配置

2012-03-07 07:10 260 查看
Dancer::Cookbook - a quick-start guide to the Dancer web framework

DESCRIPTION

A quick-start guide with examples to get you up and running with the Dancer web framework.

BEGINNER'S DANCE

Your first Dancer web app

Dancer has been designed to be easy to work with - it's trivial to write a simple web app, but still has the power to work with larger projects. To start with, let's make an incredibly simple "Hello World" example:

#!/usr/bin/perl
use Dancer;
get '/hello/:name' => sub {
return "Why, hello there " . params->{name};
};

dance;

Yes - the above is a fully-functioning web app; running that script will launch a webserver listening on the default port (3000); now you can make a request
# curl http://localhost:3000/hello/Bob Why, hello there Bob
在dancer -- introduction文章中提及到"params"的功能是让路由行为中的函数来访问传递给函数的参数。并且"params"返回的是一个hashref,然后在code段中对hashref中的name元素进行了解引用,访问其值,于是就得到了你输入的那个参数。
当然,参数也可以是空,如果是空,为了能给出更加明确的信息,我们可以使用dancer--introduction中讲解到的"标志(参数)可选性"来解决该问题,如使用dancer --introduction中的例子:
#!/usr/bin/perl
use Dancer;
get '/hello/:name?' => sub {
"Why, hello there " . (params->{name} || "whoever you are!");
};

dance;
# curl http://localhost:3000/hello/Bob Why, hello there Bob
# curl http://localhost:3000/hello/ Why, hello there whoever you are!
当访问时,有无参数的返回结果是不一样的。

(or the name of the machine you ran it on, if it's not your local system), and it will say hello. The :name part is a named parameter within the route specification, whose value is made available through params - more on that later.

Note that you don't need to use the strict and warnings pragma, they are already loaded by Dancer. (If you don't want the warnings pragma (which can lead to undesired warnings about use of undef values, for example), then set the import_warnings setting to a false value.

在dancer --introduction中也提到了当使用dancer的时候,自动引入了warnings和strict指令,但是如果你想取消warnings指令(比如:防止在使用一个未定义的变量而出现不希望的看到警告信息时,你可以参考Dancer::Config中的介绍通过设置import_warnings:false来禁用warnings指令)。
Starting a Dancer project

The first simple example is fine for trivial projects, but for anything more complex, you'll want a more maintainable solution - enter the dancer helper script, which will build the framework of your application with a single command:

# dancer -a mywebapp
+ mywebapp
+ mywebapp/config.yml+ mywebapp/environments
+ mywebapp/environments/development.yml
+ mywebapp/environments/production.yml
+ mywebapp/views+ mywebapp/views/index.tt
+ mywebapp/views/layouts
+ mywebapp/views/layouts/main.tt
+ mywebapp/mywebapp.pl+ mywebapp/lib
+ mywebapp/lib/mywebapp.pm
+ mywebapp/public+ mywebapp/public/css
+ mywebapp/public/css/style.css
+ mywebapp/public/css/error.css
+ mywebapp/public/images
+ mywebapp/public/404.html
+ mywebapp/public/dispatch.fcgi
+ mywebapp/public/dispatch.cgi
+ mywebapp/public/500.html
+ mywebapp/Makefile.PL+ mywebapp/t
+ mywebapp/t/002_index_route.t
+ mywebapp/t/001_base.t

As you can see, it creates a directory named after the name of the app, along with a configuration file, a views directory (where your templates and layouts will live), an environments directory (where environment-specific settings live), a module containing the actual guts of your application, a script to start it - or to run your web app via Plack/PSGI - more on that later.

DANCE ROUTINES: ROUTES

Declaring routes------>声明路由

To control what happens when a web request is received by your webapp, you'll need to declare routes. A route declaration indicates which HTTP method(s) it is valid for, the path it matches (e.g. /foo/bar), and a coderef to execute, which returns the response。
当有请求访问你的webapp时,为了能控制你webapp的响应,你需要声明一些路由,一个路由声明暗示了什么样的HTTP 方式时有效的,该HTTP 请求能匹配webapp路由中的哪一条,然后哪个相应的代码引用会被执行,最后会返回什么样的响应。

get '/hello/:name' => sub {
return "Hi there " . params->{name};
};

The above route specifies that, for GET requests to '/hello/...', the code block provided should be executed.
上述路由执行处理到/hello/下的GET HTTP方法,其余的HTTP 方式不能触发该路由。

Handling multiple HTTP request methods------->处理复合的HTTP请求方法

Routes can use any to match all, or a specified list of HTTP methods.
在路由行为中可使用关键字any来匹配所有的HTTP 方法或者匹配指定的多个HTTP方法。
The following will match any HTTP request to the path /myaction:

any '/myaction' => sub {
# code
}
上述代码匹配了目的地是/myaction的所有的HTTP方法的路由。
The following will match GET or POST requests to /myaction:

any ['get', 'post'] => '/myaction' => sub {
# code
};
上述方法匹配了目的地是/myaction的GET/POST HTTP方法。

For convenience, any route which matches GET requests will also match HEAD requests.
为了方便,任何一条匹配GET请求的路由都会匹配HEAD请求(即,对于相应的HEAD 请求也是通过该路由来执行的)。每条匹配GET请求的路由都会去匹配HEAD请求,该操作是dancer自动完成的,为什么非得去匹配HEAD请求,然后再去执行GET请求,是与HTTP协议相关。

Retrieving request parameters-------->检索请求参数

The params keyword returns a hashref of request parameters; these will be parameters supplied on the query string, within the path itself (with named placeholders), and, for HTTP POST requests, the content of the POST body.
关键字"params"返回请求参数的一个hashref,这些参数由请求字符串中提供,包含在named占位符中,对于HTTP POST请求,返回的内容是POST的本身。

Named parameters in route path declarations-------->路由路径中的命名参数的声明

As seen above, you can use :somename in a route's path to capture part of the path; this will become available by calling params.
So, for a web app where you want to display information on a company, you might use something like:
通过上面的例子我们能够了解到我们可以使用use :somename来捕捉路由路径的一部分,然后捕捉到的内容有params来操作。
get '/company/view/:companyid' => sub {
my $company_id = params->{companyid};
# Look up the company and return appropriate page
};

Wildcard path matching and splat-------->通配符路径匹配和splat关键

You can also declare wildcards in a path, and retrieve the values they matched with the splat keyword:
在dancer --introduction中提及到在路由路径中使用通配符,对于通配符匹配的数据被返回到一个arrayref中,然后后续使用splat来操作该arrayref。

get '/*/*' => sub {
my ($action, $id) = splat;
if (my $action eq 'view') {
return display_item($id);
} elsif ($action eq 'delete') {
return delete_item($id);
} else {
status 'not_found';
return "What?";
}
};

Before hooks - processed before a request------>前过滤(在处理请求前执行)

A before hook declares code which should be handled before a request is passed to the appropriate route.
本文出自 “灿烂的笑” 博客,请务必保留此出处http://perfect.blog.51cto.com/650470/934820
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: