File Coverage

blib/lib/Dancer/Plugin/Controller.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Controller;
2              
3             our $VERSION = '0.15';
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 '0.15';
17              
18             use YouApp::Action::Index;
19              
20              
21             get '/' => sub {
22             controller(
23             action => 'Index', # lib//Index.pm
24             template => 'index', # views/index.[tt|tpl|...]
25             layout => 'page_custom_layout', # if you need other than default layout
26             redirect_404 => '404.html' # redirect to if action method return undef
27             );
28             };
29              
30              
31             # YourApp::Action::Index.pm
32            
33             sub main {
34             my ($self) = @_;
35            
36             my $params = $self->params; # $params - contains Dancer::params() and Dancer::vars()
37              
38             ...
39              
40             return $template_params_hashref;
41             }
42              
43              
44             # config.yml
45              
46             plugins:
47             "Controller":
48             # this is prefiix for module with implementation of action
49             action_prefix: 'MyActionPrefix' # default: 'Action'
50              
51             =cut
52              
53              
54 1     1   30771 use strict;
  1         2  
  1         41  
55 1     1   5 use warnings;
  1         1  
  1         31  
56 1     1   979 use utf8;
  1         14  
  1         6  
57              
58 1     1   449 use Dancer ':syntax';
  0            
  0            
59             use Dancer::Plugin;
60              
61              
62             register controller => sub {
63             my ($self, %params) = plugin_args(@_);
64              
65             my $template_name = $params{template} || '';
66             my $custom_layout = $params{layout} || '';
67             my $action_name = $params{action} || '';
68             my $redirect_404 = $params{redirect_404} || '';
69            
70             my $conf = plugin_setting();
71             my $action_prefix = $conf->{action_prefix} || 'Action';
72              
73             my $action_base_class = sprintf('%s::%s', Dancer::config->{appname}, $action_prefix);
74             my $action_class = sprintf('%s::%s', $action_base_class, $action_name);
75             my $action_params = {
76             Dancer::params(),
77             %{Dancer::vars()}
78             };
79            
80             my $action_result = {};
81             if ($action_name) {
82             no strict 'refs';
83             *{$action_class. '::ISA'} = ($action_base_class);
84             *{$action_class. '::params'} = sub { $_[0]->{params} };
85             use strict 'refs';
86              
87             my $action_obj = bless { params => $action_params }, $action_class;
88             $action_result = $action_obj->main;
89             }
90              
91             if (not defined $action_result and $redirect_404) {
92             return redirect $redirect_404;
93             }
94             else {
95             if ($template_name) {
96             return Dancer::template(
97             $template_name,
98             $action_result,
99             { layout => $custom_layout || Dancer::config->{layout} }
100             );
101             }
102             else {
103             return $action_result;
104             }
105             }
106             };
107              
108             register_plugin;
109              
110              
111             1;
112              
113             =head1 AUTHOR
114              
115             Mikhail N Bogdanov C<< >>
116              
117             =cut