前过滤声明的代码在请求被传递到合适的路径之前进行过滤。
hook 'before' => sub {
var note => 'Hi there';
request->path('/foo/oversee')
};
get '/foo/*' => sub {
my ($match) = splat; # 'oversee';
vars->{note}; # 'Hi there'
};
The above declares a before hook which uses var to set a variable which will later be available within the route handler, then amends the path of the request to /foo/oversee; this means that, whatever path was requested, it will be treated as though the path requested was /foo/oversee.
和重定向差不多,不管是什么样的路径,都会被作为访问路径/foo/oversee来对待。
Default route------->默认路由
In case you want to avoid a 404 error, or handle multiple routes in the same way and you don't feel like configuring all of them, you can set up a default route handler.
The default route handler will handle any request that doesn't get served by any other route.
All you need to do is set up the following route as the last route:
any qr{.*} => sub {
status 'not_found';
template 'special_404', { path => request->path };
};
Then you can set up the template as such:
You tried to reach <% path %>, but it is unavailable at the moment. Please try again or contact us at our email at <...>.
Using the auto_page feature for automatic route creation
For simple "static" pages, you can simply enable the auto_page config setting; this means that you need not declare a route handler for those pages; if a request is for /foo/bar, Dancer will check for a matching view (e.g. /foo/bar.tt and render it with the default layout etc if found. For full details, see the documentation for the auto_page setting.
Why should I use the Ajax plugin------->使用ajax插件
As an Ajax query is just a HTTP query, it's similar to a GET or POST route. You may ask yourself why you may want to use the ajax keyword (from the Dancer::Plugin::Ajax plugin) instead of a simple get.
Let's say you have a path like '/user/:user' in your application. You may want to be able to serve this page, with a layout and HTML content. But you may also want to be able to call this same url from a javascript query using Ajax.
So, instead of having the following code:
get '/user/:user' => sub {
if (request->is_ajax) {
# create xml, set headers to text/xml, blablabla header('Content-Type' => 'text/xml');
header('Cache-Control' => 'no-store, no-cache, must-revalidate'); to_xml({...})
}else{
template users, {....}
}
};
you can have
get '/user/:user' => sub {
template users, {...}
}
and
ajax '/user/:user' => sub {
to_xml({...}, RootName => undef);
}
Because it's an ajax query, you know you need to return a xml content, so the content type of the response is set for you.
Using the prefix feature to split your application-------->使用前缀特性分割应用
For better maintainability, you may want to separate some of your application components to different packages. Let's say we have a simple web app with an admin section, and want to maintain this in a different package:
package myapp;
use Dancer ':syntax';
use myapp::admin;
prefix undef;
get '/' => sub {...};
1;
package myapp::admin;
use Dancer ':syntax';
prefix '/admin';
get '/' => sub {...};
1;
The following routes will be generated for us:
- get /
- get /admin/
- head /
- head /admin/
MUSCLE MEMORY: STORING DATA
Handling sessions------>处理会话
It's common to want to use sessions to give your web applications state; for instance, allowing a user to log in, creating a session, and checking that session on subsequent requests.
To make use of sessions, you must first enable the session engine - pick the session engine you want to use, then declare it in your config file: config file, add:
session: Simple
The Dancer::Session::Simple backend implements very simple in-memory session storage. This will be fast and useful for testing, but sessions do not persist between restarts of your app.
Dancer::Session::Simple backend将会话存储在内容中,会话不持久,重启服务会话消失。
You can also use the Dancer::Session::YAML backend included with Dancer, which stores session data on disc in YAML files (since YAML is a nice human-readable format, it makes inspecting the contents of sessions a breeze):
Dancer::Session::YAML将会话数据存储在磁盘中的YAML文件中。由于YAML文件是编排格式易读的文件,对于检索里面会话的内容非常容易。
session: YAML
Or, to enable session support from within your code,
或者在代码中启用会话引擎。
set session => 'YAML';
(Controlling settings is best done from your config file, though). 'YAML' in the example is the session backend to use; this is shorthand for Dancer::Session::YAML. There are other session backends you may wish to use, for instance Dancer::Session::Memcache, but the YAML backend is a simple and easy to use example which stores session data in a YAML file in sessions).
You can then use the session keyword to manipulate the session:
使用关键字session来操作会话
Storing data in the session
Storing data in the session is as easy as:
保存会话
session varname => 'value';
Retrieving data from the session
检索会话
Retrieving data from the session is as easy as:
session('varname') Or, alternatively, session->{varname}
Controlling where sessions are stored-------->会话存储地
For disc-based session back ends like Dancer::Session::YAML, Dancer::Session::Storable etc, session files are written to the session dir specified by the session_dir setting, which defaults to appdir/sessions if not specifically set.
默认情况下,对于将会话保存在磁盘文件中的引擎,将会话保存在appdir/sessions目录下,但是可以通过设置session_dir来修改保存路径。
If you need to control where session files are created, you can do so quickly and easily within your config file, for example:
session_dir: /tmp/dancer-sessions
If the directory you specify does not exist, Dancer will attempt to create it for you.
如果制定的保存会话的目录不存在,那么dancer会尝试去创建她。
Destroying a session