Mojolicious::Routes::Pattern

简介

use Mojolicious::Routes::Pattern;

# Create pattern
my $pattern = Mojolicious::Routes::Pattern->new('/test/:name');

# Match routes
my $captures = $pattern->match('/test/sebastian');
say $captures->{name};

Mojolicious::Routes::Pattern是Mojolicious::Routes的核心。

属性

constraints

my $constraints = $pattern->constraints;
$pattern        = $pattern->constraints({foo => qr/\w+/});

正则表达式约束。

defaults

my $defaults = $pattern->defaults;
$pattern     = $pattern->defaults({foo => 'bar'});

默认参数。

placeholder_start

my $start = $pattern->placeholder_start;
$pattern  = $pattern->placeholder_start(':');

表示占位符的起始字符。默认为:

placeholders

my $placeholders = $pattern->placeholders;
$pattern         = $pattern->placeholders(['foo', 'bar']);

占位符名称。

quote_end

my $end  = $pattern->quote_end;
$pattern = $pattern->quote_end(']');

表示引用的占位符的结尾字符,默认为)

quote_start

my $start = $pattern->quote_start;
$pattern  = $pattern->quote_start('[');

表示引用的占位符的开始字符,默认为(

relaxed_start

my $start = $pattern->relaxed_start;
$pattern  = $pattern->relaxed_start('*');

表示一个非严格占位符的开始字符,默认为#

tree

my $tree = $pattern->tree;
$pattern = $pattern->tree([['text', '/foo']]);

模式解析后的结果数据。

unparsed

my $unparsed = $pattern->unparsed;
$pattern     = $pattern->unparsed('/(foo)/(bar)');

非解析的原始模式。

wildcard_start

my $start = $pattern->wildcard_start;
$pattern  = $pattern->wildcard_start('*');

表示通配占位符的开始字符,默认为*

方法

Mojolicous::Routes::Route继承了Mojo::Base中的所有方法,并实现以下方法。

match

my $captures = $pattern->match('/foo/bar');
my $captures = $pattern->match('/foo/bar', 1);

把当前模式与路径匹配,默认情况下禁用格式检测。

match_partial

my $captures = $pattern->match_partial(\$path);
my $captures = $pattern->match_partial(\$path, 1);

把当前模式与路径匹配,并删除匹配部分,默认情况下禁用格式检测。

new

my $pattern = Mojolicious::Routes::Pattern->new;
my $pattern = Mojolicious::Routes::Pattern->new('/:action');
my $pattern  = Mojolicious::Routes::Pattern->new('/:action', action => qr/\w+/);
my $pattern = Mojolicious::Routes::Pattern->new(format => 0);

构建一个新的Mojolicious::Routes::Pattern对象,传参时会把参数录成模式进行解析。

parse

$pattern = $pattern->parse('/:action');
$pattern = $pattern->parse('/:action', action => qr/\w+/);
$pattern = $pattern->parse(format => 0);

解析模式,生成内部数据。

render

my $path = $pattern->render({action => 'foo'});
my $path = $pattern->render({action => 'foo'}, 1);

将模式渲染为具有参数的路径,默认情况下禁用格式化渲染。

你可能感兴趣的:(Mojolicious::Routes::Pattern)