| 1 | package Dancer::Plugin::Controller; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Dancer::Plugin::Controller - interface between a model and view | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # YourApp.pm | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Dancer ':syntax'; | ||||||||||||||||||||||||||||
| 16 | use Dancer::Plugin::Controller; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | get '/' => sub { | ||||||||||||||||||||||||||||
| 19 | controller( | ||||||||||||||||||||||||||||
| 20 | action => 'Index', # lib/<config:action_prefix>/Index.pm | ||||||||||||||||||||||||||||
| 21 | template => 'index', # views/index.[tt|tpl|...] | ||||||||||||||||||||||||||||
| 22 | layout => 'page_custom_layout', # if you need other than default layout | ||||||||||||||||||||||||||||
| 23 | redirect_404 => '404.html' # redirect to if action method return undef | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # YourApp::Action::Index.pm | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub main { | ||||||||||||||||||||||||||||
| 31 | my ($class, $params) = @_; # $params - contains Dancer::params() and Dancer::vars() | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | ... | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | return $template_params_hashref; | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # config.yml | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | plugins: | ||||||||||||||||||||||||||||
| 42 | "Controller": | ||||||||||||||||||||||||||||
| 43 | # this is prefiix for module with implementation of action | ||||||||||||||||||||||||||||
| 44 | action_prefix: 'MyActionPrefix' # default: 'Action' | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | register controller => sub { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my ($self, %params) = plugin_args(@_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $template_name = $params{template} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params{'template'} || ''
MC/DC0%0/1
$params{'template'} || ''
$params{'template'}
Truth table0%0/2
A
A= $params{'template'}
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $custom_layout = $params{layout} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params{'layout'} || ''
MC/DC0%0/1
$params{'layout'} || ''
$params{'layout'}
Truth table0%0/2
A
A= $params{'layout'}
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $action_name = $params{action} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params{'action'} || ''
MC/DC0%0/1
$params{'action'} || ''
$params{'action'}
Truth table0%0/2
A
A= $params{'action'}
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $redirect_404 = $params{redirect_404} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$params{'redirect_404'} || ''
MC/DC0%0/1
$params{'redirect_404'} || ''
$params{'redirect_404'}
Truth table0%0/2
A
A= $params{'redirect_404'}
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | my $conf = plugin_setting(); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | my $action_prefix = $conf->{action_prefix} || 'Action'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$conf->{'action_prefix'} || 'Action'
MC/DC0%0/1
$conf->{'action_prefix'} || 'Action'
$conf->{'action_prefix'}
Truth table0%0/2
A
A= $conf->{'action_prefix'}
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $action_class = sprintf('%s::%s::%s', Dancer::config->{appname}, $action_prefix, $action_name); | |||||||||||||||||||||||||||
| 69 | my $action_params = { | ||||||||||||||||||||||||||||
| 70 | Dancer::params(), | ||||||||||||||||||||||||||||
| 71 | 0 | %{Dancer::vars()} | |||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $action_result = $action_name ? $action_class->main($action_params) : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (not defined $action_result and $redirect_404) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $action_result and $redirect_404
MC/DC0%0/2
not defined $action_result and $redirect_404
not defined $action_result
$redirect_404
Truth table0%0/3
not(A) and B
A= not defined $action_resultB= $redirect_404
| |||||||||||||||||||||||||||||
| 77 | 0 | return redirect $redirect_404; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($template_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | return Dancer::template( | ||||||||||||||||||||||||||||
| 82 | $template_name, | ||||||||||||||||||||||||||||
| 83 | $action_result, | ||||||||||||||||||||||||||||
| 84 | { layout => $custom_layout || Dancer::config->{layout} } | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$custom_layout || Dancer::config()->{'layout'}
MC/DC0%0/2
$custom_layout || Dancer::config()->{'layout'}
$custom_layout
Dancer::config()->{'layout'}
Truth table0%0/3
A || B
A= $custom_layoutB= Dancer::config()->{'layout'}
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 0 | return $action_result; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1 | }; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | register_plugin; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | 1; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Mikhail N Bogdanov C<< <mbogdanov at cpan.org> >> | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||