CatalystX::Imports - Shortcut functions for Catalyst controllers
0.04
package MyApp::Controller::User; use base 'Catalyst::Controller'; use CatalystX::Imports Context => { Default => [qw( :all )], Config => [{model => 'model_name'}, 'template'] }, Vars => { Stash => [qw( $user $user_rs $template )], Session => [qw( @shown_users )], Flash => [qw( $message )] }; sub list: Chained { $user_rs = model(model_name)->search_rs; } sub load: Chained PathPart('') CaptureArgs(1) { $user = model(model_name)->find($args[0]); } sub view: Chained('load') { push @shown_users, $user->id; $template = template; } sub edit: Chained('load') { if (validate_params(request->params)) { $user->update(request->params); $message = "user updated"; } } 1;
This module is not stable yet. Features may change.
This module exports commonly used functionality and shortcuts to Catalysts own feature set into your controller. Currently, these groups of exports are available:
See also CatalystX::Imports::Context. This will export functions into your namespace that will allow you to access common methods and values easier. As an example see the uses of stash, model and args in the "SYNOPSIS".
You can ask for these imports by specifying a Context argument on the use line:
Context
use
use CatalystX::Imports Context => ...
The Config library is a special case that has no predefined exports, but allows you to import accessors to your local controller configuration.
Config
See also CatalystX::Imports::Vars. With this module, you can import the $self, $ctx and @args variables as if you'd have done
$self
$ctx
@args
my ($self, $ctx, @args) = @_;
in one of your actions. It also allows you to import variables bound to values in the stash, flash or session stores, like shown in the "SYNOPSIS".
You can use this functionality via the Vars argument on the use line:
Vars
use CatalystX::Imports Vars => ...
This is a method used by all subclasses. When called, it fetches the caller as target (the useing class) and passes it to the export_into method that must be implemented by a useable class.
export_into
It also makes sure that "install_action_wrap_into" is called after the initial runtime of your controller.
Takes a code reference and a target and registers the reference to be a wrapper for action code. As an example, without any functionality:
CatalystX::Imports->register_action_wrap_in($class, sub { my $code = shift; my @wrappers = @{ shift(@_) }; # ... put your code here ... if (my $wrapper = shift @wrappers) { return $wrapper->($code, [@wrappers], @_); } else { return $code->(@_); } });
This module needs a few parts of data to provide it's functionality. Namely, the current controller and context object, as well as the arguments to the last called action. To get to these, it will simply wrap all action code in your controller. This is what this function does, essentially.
Tells every specified exporter class (Context, etc.) to export themselves and passes their respective arguments.
Some functionality will allow you to prefix used components with a configurable string. They will use this method to find a component according to the current configuration.
See also "DIAGNOSTICS" in CatalystX::Imports::Context and "DIAGNOSTICS" in CatalystX::Imports::Vars for further messages.
The use line expects a set of key/value pairs as arguments, but you gave it a list with an odd number of elements.
Catalyst, CatalystX::Imports::Context, CatalystX::Imports::Vars
Robert 'phaylon' Sedlacek <rs@474.at>
<rs@474.at>
This program is free software; you can redistribute it and/or modify it under the same terms as perl itself.
To install CatalystX::Imports, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CatalystX::Imports
CPAN shell
perl -MCPAN -e shell install CatalystX::Imports
For more information on module installation, please visit the detailed CPAN module installation guide.