NAME
CatalystX::Imports::Context::Default - Default Context Library
BASE CLASSES
SYNOPSIS
package MyApp::Controller::Foo;
use base 'Catalyst::Controller';
use CatalystX::Imports Context => ':all';
sub foo: Local {
stash( rs => model('Foo')->find(param('foo')) );
}
1;
DESCRIPTION
This package represents the default library of Context exports.
Tags
There are some tags you can use to import groups of functions:
:all
-
Imports all registered exports in their real names. Aliases will not be included in the export. You still have to specify them explicitly.
:intro
-
All exports in the "INTROSPECTION EXPORTS" section.
:mvc
-
The
"model"
,"view"
and"controller"
exports. :req
-
Exports all functions in the "REQUEST EXPORTS" section.
:param
-
Contains the
"has_param"
and"param"
exports. :log
-
Everything defined in the "LOGGING AND DEBUGGING EXPORTS" section.
:debug
-
The "debug" and "log_debug" exports.
For more information on the import syntax, please consult CatalystX::Imports::Context.
INTROSPECTION EXPORTS
action
The action
function is a shortcut to lookup action(chain) objects. When called without arguments:
my $current_action_object = action;
it will return the current action as stored in $c->action
. You can also specify the action of which you'd like to have the object. It accepts simple action names (not starting with a slash) to return an action relative to the current controller:
my $action = action('list');
But it also allows you to pass an absolute action path:
my $action = action('/foo/bar/edit');
model
view
controller
These three functions are shortcuts to the corresponding method on the context object. Therefore, the expression
my $person = model('DBIC::Person')->find(23);
will usually do the same as
my $person = $c->model('DBIC::Person')->find(23);
Note, however, that these three exports are aware of the component_prefix configuration setting.
uri_for
See also "$c->uri_for" in Catalyst. Here is an example in combination with "action":
my $edit_uri = uri_for(action('edit'), 23);
path_to
See also "$c->path_to" in Catalyst. This utility function builds a path by your specification, starting at the application root:
my $pdf_dir = path_to(qw( root data pdfs ));
REQUEST EXPORTS
stash
See "$c->stash" in Catalyst, for which this function is a shortcut:
stash(rs => $result_set); # stores the key 'rs' in the stash
...
my $rs = stash->{rs}; # retrieves it again
arguments
Returns the last called action's passed arguments.
request
Returns the current Catalyst::Request object. You can also import its alias req
.
if (request->method eq 'POST') {
...
}
response
Returns the current Catalyst::Response object. You can also import its alias res
.
response->status(404);
response->body('I misplaced that resource.');
captures
Returns the current requests captures.
has_param
Boolean test if a query parameter was submitted with the request.
sub search: Local {
if (has_param('q')) {
my $q = param('q');
stash( result => model('Foo')->search({ bar => $q }) );
}
}
param
The same as a call to the param
method on the current Catalyst::Request object.
LOGGING AND DEBUGGING EXPORTS
debug
This function allows you to nest debugging code directly in your actions and only execute it when debugging is turned on.
debug { # runs only in debug mode
my $foo = 'something';
my @bar = (1 .. 10_000);
warn "Doing $foo on $_" for @bar;
log_debug('Done.');
};
log_debug
Outputs content to the logging channel, but only if the application is in debug mode.
log_info
log_warn
log_error
These functions log to the info
, warn
and error
channels.
SEE ALSO
Catalyst, CatalystX::Imports::Context, CatalystX::Imports
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.