NAME

CatalystX::Imports - Shortcut functions for Catalyst controllers

VERSION

0.04

SYNOPSIS

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;

DESCRIPTION

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:

Context Exports

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:

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.

Variable Exports

See also CatalystX::Imports::Vars. With this module, you can import the $self, $ctx and @args variables as if you'd have done

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:

use CatalystX::Imports Vars => ...

METHODS

import

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.

It also makes sure that "install_action_wrap_into" is called after the initial runtime of your controller.

register_action_wrap_in

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->(@_);
    }
});

install_action_wrap_into

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.

export_into

Tells every specified exporter class (Context, etc.) to export themselves and passes their respective arguments.

_ensure_class_loaded

Convenience method that tries to requires the passed package if it isn't already loaded. Will return -1 if it had to be required, and 1 if it was already loaded.

resolve_component

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.

DIAGNOSTICS

See also "DIAGNOSTICS" in CatalystX::Imports::Context and "DIAGNOSTICS" in CatalystX::Imports::Vars for further messages.

CatalystX::Imports expects a key/value list as argument

The use line expects a set of key/value pairs as arguments, but you gave it a list with an odd number of elements.

SEE ALSO

Catalyst, CatalystX::Imports::Context, CatalystX::Imports::Vars

AUTHOR AND COPYRIGHT

Robert 'phaylon' Sedlacek <rs@474.at>

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as perl itself.