NAME
Catalyst::Plugin::Hooks - Add hooks to Catalyst engine actions
SYNOPSIS
In MyApp.pm:
use Catalyst qw(
-Debug
Hooks
);
In Some model:
sub new {
my $self = shift;
my ( $c ) = @_;
$self->NEXT::new( @_ );
open my $filehandle, "> foo.log";
$c->add_after_finalize_hook( sub {
my $c = shift;
$filehandle->flush();
$c->log->info( "Flushed filehandle after finalize" );
} );
return $self;
}
DESCRIPTION
This Plugin is usefull for when you want to run some code before or after a catalyst engine action. Consider writing a Catalyst plugin if you implement more general functionality. But let's say you want to flush your log's filehandle after the request is done (then the requestor doesn't have to wait for your log's to be flushed). It would be nice to put the code for flushing the filehandle next to the rest of the code that's bothered with the filehandle, so you don't have to pass the filehandle around. Example for this is shown in the "SYNOPSIS".
Hooks are addable everywhere a $c exists, even in Controllers. But remember, adding a hook every request will cause a memory overflow. So don't put these methods in Controller actions.
METHODS
All of these methods are currently hookable:
handle_request
prepare
prepare_request
prepare_connection
prepare_query_parameters
prepare_headers
prepare_cookies
prepare_path
prepare_body
prepare_body_parameters
prepare_parameters
prepare_uploads
prepare_action
dispatch
finalize
finalize_uploads
finalize_error
finalize_headers
finalize_cookies
finalize_body
To add a before hook, call $c->add_ <method name> _hook( sub { some code } );
To add an after hook, call $c->add_after_ <method name> _hook( sub { some code } );
$c->add_before_ <method name> _hook
is an alias to $c->add_ <method name> _hook
.
SEE ALSO
Catalyst, Catalyst::Manual::Internals for when the different actions are called.
AUTHOR
Berik Visschers <berikv@xs4all.nl>