简介
use Mojolicious::Routes::Route;
my $r = Mojolicious::Routes::Route->new;
Mojolicious::Routes::Route是Mojolicious::Routes使用的路由窗口。
属性
children
my $children = $r->children;
$r = $r->children([Mojolicious::Routes::Route->new]);
当前路由的子路由,用于构建一个嵌套的路由。
inline
my $bool = $r->inline;
$r = $r->inline($bool);
允许在当前路由器对象中使用 under 方法。
parent
my $parent = $r->parent;
$r = $r->parent(Mojolicious::Routes::Route->new);
当前路由的父路由,通过是一个Mojolicious::Routes::Route对象。
partial
my $bool = $r->partial;
$r = $r->partial($bool);
路由没有特定的结束,剩下的字符将被作为path。
pattern
my $pattern = $r->pattern;
$r = $r->pattern(Mojolicious::Routes::Pattern->new);
当前路由器的模式,默认为Mojolicious::Routes::Pattern对象。
方法
Mojolicious::Routes::Route从Mojo::Base继承了所有的方法,并实现以下方法。
add_child
$r = $r->add_child(Mojolicious::Routes::Route->new);
将一个孩子添加到此路由。被添加的对象如果有父对象,它将自动从其当前父对象中删除。
any
my $route = $r->any;
my $route = $r->any('/:foo');
my $route = $r->any('/:foo' => sub {...});
my $route = $r->any('/:foo' => sub {...} => 'name');
my $route = $r->any('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->any('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->any('/:foo' => (agent => qr/Firefox/) => sub {...});
my $route = $r->any(['GET', 'POST'] => '/:foo' => sub {...});
my $route = $r->any(['GET', 'POST'] => '/:foo' => [foo => qr/\w+/]);
为匹配的所有HTTP请求方法生成Mojolicious::Routes::Routec对象。
# Route with pattern and destination
$r->any('/user')->to('user#whatever');
所有参数都是可选的,但些参数必须以特定的顺序出现。例如两个数组的引用,前一个表示包含的HTTP方法,后面一个用于对点位符的值进行限制。
# Route with HTTP methods, pattern, restrictive placeholders and destination
$r->any(['DELETE', 'PUT'] => '/:foo' => [foo => qr/\w+/])->to('foo#bar');
还有两个字符串参数,前一个指定路由的模式,后一个指定路由的名称。默认情况下模式为/
。
# Route with pattern, name and destination
$r->any('/:foo' => 'foo_route')->to('foo#bar');
在路由模式和名称之间,可以使用任意数量的key-value来指定路由条件。
# Route with pattern, condition and destination
$r->any('/' => (agent => qr/Firefox/))->to('foo#bar');
hashref用于指定可选的点位符,和其默认值。
# Route with pattern, optional placeholder and destination
$r->any('/:foo' => {foo => 'bar'})->to('foo#bar');
并且可以使用代码引用来指定回调函数,回调函数会被合并到存储默认值的stash中。
# Route with pattern and a closure as destination
$r->any('/:foo' => sub {
my $c = shift;
$c->render(text => 'Hello World!');
});
delete
my $route = $r->delete;
my $route = $r->delete('/:foo');
my $route = $r->delete('/:foo' => sub {...});
my $route = $r->delete('/:foo' => sub {...} => 'name');
my $route = $r->delete('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->delete('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->delete('/:foo' => (agent => qr/Firefox/) => sub {...});
为HTTP方法为DELETE的REQUEST生成Mojolicious::Routes::Route对象。除了默认已经确定的HTTP方法为 DELETE不用传递,其他参数与any方法的完全相同。
# Route with destination
$r->delete('/user')->to('user#remove');
detour
$r = $r->detour(action => 'foo');
$r = $r->detour('controller#action');
$r = $r->detour(Mojolicious->new, foo => 'bar');
$r = $r->detour('MyApp', {foo => 'bar'});
设置此路径的默认参数,并允许部分匹配以简化应用的嵌入。使用与to方法相同的参数。
find
my $route = $r->find('foo');
按名称查找子路由,自定义名称优于自动生成的名称。
get
my $route = $r->get;
my $route = $r->get('/:foo');
my $route = $r->get('/:foo' => sub {...});
my $route = $r->get('/:foo' => sub {...} => 'name');
my $route = $r->get('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->get('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->get('/:foo' => (agent => qr/Firefox/) => sub {...});
为HTTP方法为GET的REQUEST生成Mojolicious::Routes::Route对象。除了默认已经确定的HTTP方法为 GET不用传递,其他参数与any方法的完全相同。
# Route with destination
$r->get('/user')->to('user#show');
has_custom_name
my $bool = $r->has_custom_name;
检查路由是否具有自定义名称。
has_websocket
my $bool = $r->has_websocket;
检查此路由是否具有WebSocket祖先,并将结果缓存以备将来使用。
is_endpoint
my $bool = $r->is_endpoint;
检查此路由是否为端点路由。没有子路由的路由就是端点路由。
is_websocket
my $bool = $r->is_websocket;
检查此路由是否为WebSocket。
name
my $name = $r->name;
$r = $r->name('foo');
获取或设置当前路由的名称。默认为基于路由模式自动生成的名称。注:名称current
是一个用于指定当前路由的保留字。
options
my $route = $r->options;
my $route = $r->options('/:foo');
my $route = $r->options('/:foo' => sub {...});
my $route = $r->options('/:foo' => sub {...} => 'name');
my $route = $r->options('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->options('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->options('/:foo' => (agent => qr/Firefox/) => sub {...});
为HTTP方法为OPTION的REQUEST生成Mojolicious::Routes::Route对象。除了默认已经确定的HTTP方法为 OPTION不用传递,其他参数与any方法的完全相同。
# Route with destination
$r->options('/user')->to('user#overview');
over
my $over = $r->over;
$r = $r->over(foo => 1);
$r = $r->over(foo => 1, bar => {baz => 'yada'});
$r = $r->over([foo => 1, bar => {baz => 'yada'}]);
激活当前路由的条件。注:这将关闭路由缓存,因为对于条件来说缓存太复杂了。
# Route with condition and destination
$r->get('/foo')->over(host => qr/mojolicious\.org/)->to('foo#bar');
parse
$r = $r->parse('/:action');
$r = $r->parse('/:action', action => qr/\w+/);
$r = $r->parse(format => 0);
patch
my $route = $r->patch;
my $route = $r->patch('/:foo');
my $route = $r->patch('/:foo' => sub {...});
my $route = $r->patch('/:foo' => sub {...} => 'name');
my $route = $r->patch('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->patch('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->patch('/:foo' => (agent => qr/Firefox/) => sub {...});
为HTTP方法为PATCH的REQUEST生成Mojolicious::Routes::Route对象。除了默认已经确定的HTTP方法为 PATCH不用传递,其他参数与any方法的完全相同。
# Route with destination
$r->patch('/user')->to('user#update');
post
my $route = $r->post;
my $route = $r->post('/:foo');
my $route = $r->post('/:foo' => sub {...});
my $route = $r->post('/:foo' => sub {...} => 'name');
my $route = $r->post('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->post('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->post('/:foo' => (agent => qr/Firefox/) => sub {...});
为HTTP方法为POST的REQUEST生成Mojolicious::Routes::Route对象。除了默认已经确定的HTTP方法为 POST不用传递,其他参数与any方法的完全相同。
# Route with destination
$r->post('/user')->to('user#create');
put
my $route = $r->put;
my $route = $r->put('/:foo');
my $route = $r->put('/:foo' => sub {...});
my $route = $r->put('/:foo' => sub {...} => 'name');
my $route = $r->put('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->put('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->put('/:foo' => (agent => qr/Firefox/) => sub {...});
为HTTP方法为PUT的REQUEST生成Mojolicious::Routes::Route对象。除了默认已经确定的HTTP方法为 PUT不用传递,其他参数与any方法的完全相同。
# Route with destination
$r->put('/user')->to('user#replace');
remove
$r = $r->remove;
把当前路由,从父路由中删除。
# Remove route completely
$r->find('foo')->remove;
# Reattach route to new parent
$r->route('/foo')->add_child($r->find('bar')->remove);
render
my $path = $r->render({foo => 'bar'});
将参数渲染到路径中。
root
my $root = $r->root;
将返回一个Mojolicious::Routes对象,当前对象是其后代。
route
my $route = $r->route;
my $route = $r->route('/:action');
my $route = $r->route('/:action', action => qr/\w+/);
my $route = $r->route(format => 0);
用于匹配所有HTTP请求方法的路由生成器,返回一个Mojolicious::Routes::Route对象。
suggested_method
my $method = $r->suggested_method;
建议到达此路由的HTTP方法,GET和POST是首选项。
to
my $defaults = $r->to;
$r = $r->to(action => 'foo');
$r = $r->to({action => 'foo'});
$r = $r->to('controller#action');
$r = $r->to('controller#action', foo => 'bar');
$r = $r->to('controller#action', {foo => 'bar'});
$r = $r->to(Mojolicious->new);
$r = $r->to(Mojolicious->new, foo => 'bar');
$r = $r->to(Mojolicious->new, {foo => 'bar'});
$r = $r->to('MyApp');
$r = $r->to('MyApp', foo => 'bar');
$r = $r->to('MyApp', {foo => 'bar'});
设置此路由的默认参数。
to_string
my $str = $r->to_string;
根据当前路径生成字符串表示。
under
my $route = $r->under(sub {...});
my $route = $r->under('/:foo' => sub {...});
my $route = $r->under('/:foo' => {foo => 'bar'});
my $route = $r->under('/:foo' => [foo => qr/\w+/]);
my $route = $r->under('/:foo' => (agent => qr/Firefox/));
my $route = $r->under([format => 0]);
为具有中间目的地的路由生成Mojolicious::Routes::Route对象。除了HTTP方法已经默认为under外,其他参数与any方法完全相同。
# Intermediate destination and prefix shared between two routes
my $auth = $r->under('/user')->to('user#auth');
$auth->get('/show')->to('#show');
$auth->post('/create')->to('#create');
via
my $methods = $r->via;
$r = $r->via('GET');
$r = $r->via('GET', 'POST');
$r = $r->via(['GET', 'POST']);
限制允许此路由处理的HTTP方法,默认为无限制。
websocket
my $route = $r->websocket;
my $route = $r->websocket('/:foo');
my $route = $r->websocket('/:foo' => sub {...});
my $route = $r->websocket('/:foo' => sub {...} => 'name');
my $route = $r->websocket('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->websocket('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->websocket('/:foo' => (agent => qr/Firefox/) => sub {...});
为WebSocket的握手生成Mojolicious::Routes::Route对象。除了设置了websocket标识外,与get方法的参数和工作方式完全相同。
# Route with destination
$r->websocket('/echo')->to('example#echo');
a