The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Dancer::Plugin::Controller - interface between a model and view

SYNOPSIS

        # YourApp.pm

        use Dancer ':syntax';
        use Dancer::Plugin::Controller '0.15';

        use YouApp::Action::Index;


        get '/' => sub { 
                controller(
                        action       => 'Index',              # lib/<config:action_prefix>/Index.pm
                        template     => 'index',              # views/index.[tt|tpl|...]
                        layout       => 'page_custom_layout', # if you need other than default layout
                        redirect_404 => '404.html'            # redirect to if action method return undef
                );
        };


        # YourApp::Action::Index.pm
        
        sub main {
                my ($self) = @_;
                
                my $params = $self->params; # $params - contains Dancer::params() and Dancer::vars()

                ...

                return $template_params_hashref;
        }


        # config.yml

        plugins:
                "Controller":
                        # this is prefiix for module with implementation of action
                        action_prefix: 'MyActionPrefix' # default: 'Action'

AUTHOR

Mikhail N Bogdanov <mbogdanov at cpan.org>