in reply to Re^5: Dancer2 hook
in thread Dancer2 hook

package Howdy; use Dancer2; use Dancer2::Plugin::TestHook; get '/' => sub { template 'index' => { 'title' => 'Howdy' }; }; get '/hello' => sub { die "We are out of ponies\n"; return "Welcome to Dancer2 Web Application"; }; true;

Replies are listed 'Best First'.
Re^7: Dancer2 hook ( Dancer2::Plugin BUILD)
by Anonymous Monk on Aug 13, 2019 at 02:25 UTC

    Hi

    That doesn't compile

    Also, it is still incomplete

    Still, if I go ahead and fill in the blanks by copying  dancer2 -a  HookTest I think I see what you see

    You should read https://metacpan.org/pod/Dancer2::Plugin#Writing-the-plugin

    This triggers

    package TestHook; use Dancer2::Plugin; sub BUILD { my( $plugin ) = @_; $plugin->app->add_route( method => 'get', regexp => '/die', code => sub { die "get '/die' => sub { " }, ); $plugin->app->add_route( method => 'get', regexp => '/ride', code => sub { 'ride' }, ); $plugin->app->add_hook( Dancer2::Core::Hook->new( name => 'after_error', #~ code => sub { $_[0]->content( $_[0]->content . " ... please +?" ) } code => sub { my $content = shift; warn ' From TestHook From TestHook From TestHo +ok From TestHook From TestHook '; $content->{content} .= " hello world From TestHook + "; } )); } 1; __END__