| 1 | package Mojolicious::Routes; | ||||||||||||||||||||||||||||
| 2 | 21 | ▶ | use Mojo::Base 'Mojolicious::Routes::Route'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use Mojo::Cache; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use Mojo::DynamicMethods; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use Mojo::Loader qw(load_class); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Mojo::Util qw(camelize); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | has base_classes => sub { [qw(Mojolicious::Controller Mojolicious)] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 17 | ▶ | has cache => sub { Mojo::Cache->new }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 104 | ▶ | has [qw(conditions shortcuts)] => sub { {} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 60 | ▶ | has types => sub { {num => qr/[0-9]+/} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | has namespaces => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 251 | ▶ | sub add_condition { $_[0]->conditions->{$_[1]} = $_[2] and return $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
add_condition: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 21 | ▶ | sub add_shortcut { | ||||||||||||||||||||||||||
|
Subroutine
add_shortcut: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 21 | my ($self, $name, $cb) = @_; | |||||||||||||||||||||||||||
| 21 | 21 | $self->shortcuts->{$name} = $cb; | |||||||||||||||||||||||||||
| 22 | 21 | Mojo::DynamicMethods::register 'Mojolicious::Routes::Route', $self, $name, $cb; | |||||||||||||||||||||||||||
| 23 | 21 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub add_type { $_[0]->types->{$_[1]} = $_[2] and return $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
add_type: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 234 | ▶ | sub continue { | ||||||||||||||||||||||||||
|
Subroutine
continue: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 234 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 234 | my $match = $c->match; | |||||||||||||||||||||||||||
| 32 | 234 | my $stack = $match->stack; | |||||||||||||||||||||||||||
| 33 | 234 | my $position = $match->position; | |||||||||||||||||||||||||||
| 34 | 234 | ▶ | return _render($c) unless my $field = $stack->[$position]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Merge captures into stash | ||||||||||||||||||||||||||||
| 37 | 132 | my $stash = $c->stash; | |||||||||||||||||||||||||||
| 38 | 132 | ▶ | @{$stash->{'mojo.captures'} //= {}}{keys %$field} = values %$field; | ||||||||||||||||||||||||||
|
Condition100%2/2
$stash->{"mojo.captures"} //= {}
MC/DC100%1/1
$stash->{"mojo.captures"} //= {}
$stash->{"mojo.captures"}
Truth table100%2/2
A
A= $stash->{"mojo.captures"}
| |||||||||||||||||||||||||||||
| 39 | 132 | @$stash{keys %$field} = values %$field; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 132 | my $continue; | |||||||||||||||||||||||||||
| 42 | 132 | my $last = !$stack->[++$position]; | |||||||||||||||||||||||||||
| 43 | 132 | ▶ | if (my $cb = $field->{cb}) { $continue = $self->_callback($c, $cb, $last) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 106 | else { $continue = $self->_controller($c, $field, $last) } | |||||||||||||||||||||||||||
| 45 | 110 | $match->position($position); | |||||||||||||||||||||||||||
| 46 | 110 | ▶ | $self->continue($c) if $last || $continue; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$last or $continue
MC/DC100%2/2
$last or $continue
$last
$continue
Truth table100%3/3
A or B
A= $lastB= $continue
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 150 | ▶ | sub dispatch { | ||||||||||||||||||||||||||
|
Subroutine
dispatch: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 150 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 51 | 150 | $self->match($c); | |||||||||||||||||||||||||||
| 52 | 150 | ▶ | @{$c->match->stack} ? $self->continue($c) : return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 101 | return 1; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 22 | ▶ | sub lookup { ($_[0]{reverse} //= $_[0]->_index)->{$_[1]} } | ||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=2 SCAR=7.2
Pod
documented
Condition66%2/3
$_[0]{"reverse"} //= $_[0]->_index
MC/DC0%0/2
$_[0]{"reverse"} //= $_[0]->_index
$_[0]{"reverse"}
$_[0]->_index
Truth table66%2/3
A //= B
A= $_[0]{"reverse"}B= $_[0]->_index
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 151 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 151 | my ($self, $c) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Path (partial path gets priority) | ||||||||||||||||||||||||||||
| 62 | 151 | my $req = $c->req; | |||||||||||||||||||||||||||
| 63 | 151 | my $path = $c->stash->{path}; | |||||||||||||||||||||||||||
| 64 | 151 | ▶ | if (defined $path) { $path = "/$path" if $path !~ m!^/! } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 65 | 150 | else { $path = $req->url->path->to_route } | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Method (HEAD will be treated as GET) | ||||||||||||||||||||||||||||
| 68 | 151 | my $method = uc $req->method; | |||||||||||||||||||||||||||
| 69 | 151 | ▶ | if ($method eq 'POST' && (my $override = $req->url->query->clone->param('_method'))) { $method = uc $override } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$method eq "POST" and my $override = $req->url->query->clone->param("_method")
MC/DC100%2/2
$method eq "POST" and my $override = $req->url->query->clone->param("_method")
$method eq "POST"
my $override = $req->url->query->clone->param("_method")
Truth table100%3/3
A and B
A= $method eq "POST"B= my $override = $req->url->query->clone->param("_method")
| |||||||||||||||||||||||||||||
| 70 | 151 | ▶ | $method = 'GET' if $method eq 'HEAD'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Check cache | ||||||||||||||||||||||||||||
| 73 | 151 | ▶ | my $ws = $c->tx->is_websocket ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 151 | my $match = $c->match; | |||||||||||||||||||||||||||
| 75 | 151 | $match->root($self); | |||||||||||||||||||||||||||
| 76 | 151 | my $cache = $self->cache; | |||||||||||||||||||||||||||
| 77 | 151 | ▶ | if (my $result = $cache->get("$method:$path:$ws")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 19 | return $match->endpoint($result->{endpoint})->stack($result->{stack}); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Check routes | ||||||||||||||||||||||||||||
| 82 | 132 | $match->find($c => {method => $method, path => $path, websocket => $ws}); | |||||||||||||||||||||||||||
| 83 | 132 | ▶ | return undef unless my $route = $match->endpoint; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 108 | $cache->set("$method:$path:$ws" => {endpoint => $route, stack => $match->stack}); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 88 | ▶ | sub _action { shift->plugins->emit_chain(around_action => @_) } | ||||||||||||||||||||||||||
|
Subroutine
_action: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 26 | ▶ | sub _callback { | ||||||||||||||||||||||||||
|
Subroutine
_callback: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 90 | 26 | my ($self, $c, $cb, $last) = @_; | |||||||||||||||||||||||||||
| 91 | 26 | ▶ | $c->stash->{'mojo.routed'} = 1 if $last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 26 | $c->helpers->log->trace('Routing to a callback'); | |||||||||||||||||||||||||||
| 93 | 26 | return _action($c->app, $c, $cb, $last); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 106 | ▶ | sub _class { | ||||||||||||||||||||||||||
|
Subroutine
_class: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 97 | 106 | my ($self, $c, $field) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Application instance | ||||||||||||||||||||||||||||
| 100 | 106 | ▶ | return $field->{app} if ref $field->{app}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Application class | ||||||||||||||||||||||||||||
| 103 | 105 | my @classes; | |||||||||||||||||||||||||||
| 104 | 105 | ▶ | my $class = $field->{controller} ? camelize $field->{controller} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($field->{app}) { push @classes, $field->{app} } | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Specific namespace | ||||||||||||||||||||||||||||
| 108 | elsif (defined(my $ns = $field->{namespace})) { | ||||||||||||||||||||||||||||
| 109 | 3 | ▶ | croak qq{Namespace "$ns" requires a controller} unless $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | push @classes, $ns ? "${ns}::$class" : $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # All namespaces | ||||||||||||||||||||||||||||
| 114 | 80 | elsif ($class) { push @classes, "${_}::$class" for @{$self->namespaces} } | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Try to load all classes | ||||||||||||||||||||||||||||
| 117 | 104 | my $log = $c->helpers->log; | |||||||||||||||||||||||||||
| 118 | 104 | for my $class (@classes) { | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Failed | ||||||||||||||||||||||||||||
| 121 | 200 | ▶ | next unless defined(my $found = $self->_load($class)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 76 | ▶ | croak qq{Class "$class" is not a controller} unless $found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Success | ||||||||||||||||||||||||||||
| 125 | 72 | return $class->new(%$c); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Nothing found | ||||||||||||||||||||||||||||
| 129 | 25 | ▶ | return @classes ? croak qq{Controller "$classes[-1]" does not exist} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 106 | ▶ | sub _controller { | ||||||||||||||||||||||||||
|
Subroutine
_controller: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 133 | 106 | my ($self, $old, $field, $last) = @_; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # Load and instantiate controller/application | ||||||||||||||||||||||||||||
| 136 | 106 | my $new; | |||||||||||||||||||||||||||
| 137 | 106 | ▶ | unless ($new = $self->_class($old, $field)) { return defined $new } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Application | ||||||||||||||||||||||||||||
| 140 | 73 | my $class = ref $new; | |||||||||||||||||||||||||||
| 141 | 73 | my $log = $old->helpers->log; | |||||||||||||||||||||||||||
| 142 | 73 | ▶ | if ($new->isa('Mojolicious')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 143 | 5 | $log->trace(qq{Routing to application "$class"}); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Try to connect routes | ||||||||||||||||||||||||||||
| 146 | 5 | ▶ | if (my $sub = $new->can('routes')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 5 | my $r = $new->$sub; | |||||||||||||||||||||||||||
| 148 | 5 | ▶ | $r->parent($old->match->endpoint) unless $r->parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 5 | $new->handler($old); | |||||||||||||||||||||||||||
| 151 | 5 | $old->stash->{'mojo.routed'} = 1; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Action | ||||||||||||||||||||||||||||
| 155 | elsif (my $method = $field->{action}) { | ||||||||||||||||||||||||||||
| 156 | 67 | $log->trace(qq{Routing to controller "$class" and action "$method"}); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 67 | ▶ | if (my $sub = $new->can($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 62 | ▶ | $old->stash->{'mojo.routed'} = 1 if $last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 62 | ▶ | return 1 if _action($old->app, $new, $sub, $last); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 5 | else { $log->trace('Action not found in controller') } | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | else { croak qq{Controller "$class" requires an action} } | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 18 | return undef; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 200 | ▶ | sub _load { | ||||||||||||||||||||||||||
|
Subroutine
_load: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 172 | 200 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # Load unless already loaded | ||||||||||||||||||||||||||||
| 175 | 200 | ▶ | return 1 if $self->{loaded}{$app}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 144 | ▶ | if (my $e = load_class $app) { ref $e ? die $e : return undef } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Check base classes | ||||||||||||||||||||||||||||
| 179 | 20 | ▶ | return 0 unless first { $app->isa($_) } @{$self->base_classes}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 16 | return $self->{loaded}{$app} = 1; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 102 | ▶ | sub _render { | ||||||||||||||||||||||||||
|
Subroutine
_render: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 184 | 102 | my $c = shift; | |||||||||||||||||||||||||||
| 185 | 102 | my $stash = $c->stash; | |||||||||||||||||||||||||||
| 186 | 102 | ▶ | return if $stash->{'mojo.rendered'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 41 | ▶ | $c->render_maybe or $stash->{'mojo.routed'} or croak 'Route without action and nothing to render'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c->render_maybe or $stash->{"mojo.routed"}
MC/DC50%1/2
$c->render_maybe or $stash->{"mojo.routed"}
$c->render_maybe
$stash->{"mojo.routed"}
Truth table66%2/3
A or B
A= $c->render_maybeB= $stash->{"mojo.routed"}
| |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 21 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =encoding utf8 | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 NAME | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Mojolicious::Routes - Always find your destination with routes | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | use Mojolicious::Routes; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Simple route | ||||||||||||||||||||||||||||
| 203 | my $r = Mojolicious::Routes->new; | ||||||||||||||||||||||||||||
| 204 | $r->any('/')->to(controller => 'blog', action => 'welcome'); | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # More advanced routes | ||||||||||||||||||||||||||||
| 207 | my $blog = $r->under('/blog'); | ||||||||||||||||||||||||||||
| 208 | $blog->get('/list')->to('blog#list'); | ||||||||||||||||||||||||||||
| 209 | $blog->get('/:id' => [id => qr/\d+/])->to('blog#show', id => 23); | ||||||||||||||||||||||||||||
| 210 | $blog->patch(sub ($c) { $c->render(text => 'Go away!', status => 405) }); | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | L<Mojolicious::Routes> is the core of the L<Mojolicious> web framework. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | See L<Mojolicious::Guides::Routing> for more. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 TYPES | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | These placeholder types are available by default. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 num | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | $r->get('/article/<id:num>'); | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | Placeholder value needs to be a non-fractional number, similar to the regular expression C<([0-9]+)>. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | L<Mojolicious::Routes> inherits all attributes from L<Mojolicious::Routes::Route> and implements the following new | ||||||||||||||||||||||||||||
| 231 | ones. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head2 base_classes | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | my $classes = $r->base_classes; | ||||||||||||||||||||||||||||
| 236 | $r = $r->base_classes(['MyApp::Controller']); | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Base classes used to identify controllers, defaults to L<Mojolicious::Controller> and L<Mojolicious>. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head2 cache | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | my $cache = $r->cache; | ||||||||||||||||||||||||||||
| 243 | $r = $r->cache(Mojo::Cache->new); | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | Routing cache, defaults to a L<Mojo::Cache> object. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head2 conditions | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | my $conditions = $r->conditions; | ||||||||||||||||||||||||||||
| 250 | $r = $r->conditions({foo => sub {...}}); | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Contains all available conditions. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =head2 namespaces | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | my $namespaces = $r->namespaces; | ||||||||||||||||||||||||||||
| 257 | $r = $r->namespaces(['MyApp::Controller', 'MyApp']); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Namespaces to load controllers from. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Add another namespace to load controllers from | ||||||||||||||||||||||||||||
| 262 | push @{$r->namespaces}, 'MyApp::MyController'; | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =head2 shortcuts | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | my $shortcuts = $r->shortcuts; | ||||||||||||||||||||||||||||
| 267 | $r = $r->shortcuts({foo => sub {...}}); | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | Contains all available shortcuts. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =head2 types | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | my $types = $r->types; | ||||||||||||||||||||||||||||
| 274 | $r = $r->types({lower => qr/[a-z]+/}); | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Registered placeholder types, by default only L</"num"> is already defined. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head1 METHODS | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | L<Mojolicious::Routes> inherits all methods from L<Mojolicious::Routes::Route> and implements the following new ones. | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =head2 add_condition | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | $r = $r->add_condition(foo => sub ($route, $c, $captures, $arg) {...}); | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Register a condition. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | $r->add_condition(foo => sub ($route, $c, $captures, $arg) { | ||||||||||||||||||||||||||||
| 289 | ... | ||||||||||||||||||||||||||||
| 290 | return 1; | ||||||||||||||||||||||||||||
| 291 | }); | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =head2 add_shortcut | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | $r = $r->add_shortcut(foo => sub ($route, @args) {...}); | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Register a shortcut. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | $r->add_shortcut(foo => sub ($route, @args) {...}); | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =head2 add_type | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | $r = $r->add_type(foo => qr/\w+/); | ||||||||||||||||||||||||||||
| 304 | $r = $r->add_type(foo => ['bar', 'baz']); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | Register a placeholder type. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | $r->add_type(lower => qr/[a-z]+/); | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =head2 continue | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | $r->continue(Mojolicious::Controller->new); | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | Continue dispatch chain and emit the hook L<Mojolicious/"around_action"> for every action. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =head2 dispatch | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | my $bool = $r->dispatch(Mojolicious::Controller->new); | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Match routes with L</"match"> and dispatch with L</"continue">. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =head2 lookup | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | my $route = $r->lookup('foo'); | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Find route by name with L<Mojolicious::Routes::Route/"find"> and cache all results for future lookups. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head2 match | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | $r->match(Mojolicious::Controller->new); | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | Match routes with L<Mojolicious::Routes::Match>. | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =cut | ||||||||||||||||||||||||||||