The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Catalyst::Plugin::CurrentComponents - Declare current components more easily.

SYNOPSIS

Use the plugin in your application class:

    package MyApp;
    use Catalyst 'CurrentComponents';

    # Optional configuration
    MyApp->config(
      'Plugin::CurrentComponents' => {
        model_instance_from_return => 1,
        view_instance_from_return => 1,
      },
    );

    MyApp->setup;

Then you can use it in your controllers:

    package MyApp::Controller::Example;

    use base 'Catalyst::Controller';

    sub current_model_instance {
      my ($self, $c) = @_;
      return $c->model("Form::Login", user_database => $c->model('Users'));
    }

    sub myaction :Local {
      my ($self, $c) = @_;
      my $c->model; # Isa 'MyApp::Model::Form::Login', or whatever that returns;
    }

    sub set_model :Local {
      my ($self, $c) = @_;
      $c->current_model_instance($c->model('Foo')); # $c->model ISA 'MyApp::Model::Foo
    }

    sub set_view :Local {
      my ($self, $c) = @_;
      $c->current_view_instance($c->view('Bar')); # $c->view ISA 'MyApp::View::Bar
    }

DESCRIPTION

This plugin gives you an alternative to setting the current_view|model(_instance) via a controller method or via context helper methods. You may find this a more readable approach than setting it via the stash.

You may also enable a global option to set the current_model_instance or the current_view_instance via the return value of an action. See "CONFIGURATION"

Please Seee documention about Views and Models in Catalyst.

METHODS

This plugin adds the following methods to your context.

current_model

Sets $c->stash->{current_model} if an argument is passed. Always returns the current value of this stash key. Expects the string name of a model.

current_model_instance

Sets $c->stash->{current_model_instance} if an argument is passed. Always returns the current value of this stash key. Expects either the instance of an already created model or can accept arguments that can be validly submitted to $c->model.

current_view

Sets $c->stash->{current_view} if an argument is passed. Always returns the current value of this stash key. Expects the string new of a view.

current_view_instance

Sets $c->stash->{current_view_instance} if an argument is passed. Always returns the current value of this stash key. Expects either the instance of an already created view or can accept arguments that can be validly submitted to $c->view.

CONTROLLER METHODS

This plugin will inspect the current controller for the following methods

current_model

current_model_instance

Same as the context methods, but lets you set this at a controller level. Useful for base classes or roles. Example:

CONFIGURATION

This plugin supports configuration under the "Plugin::CurrentComponents" key. For example:

    MyApp->config(
      'Plugin::CurrentComponents' => {
        model_instance_from_return => 1,
        view_instance_from_return => 1,
      },
    );

model_instance_from_return

Allows one to set the current_model_instance from the return value of a matched action. Please note this is an experimental option which is off by default. The return value must be a defined, blessed objected that ISA Catalyst::Model for this to work. Example:

    sub set_model_by_return :Chained(/) CaptureArgs(0) {
      my ($self, $c) = @_;
      return $c->model('CurrentModel'); # $c->model ISA 'MyApp::Model::CurrentModel'
    }

view_instance_from_return

Allows one to set the current_view_instance from the return value of a matched action. Please note this is an experimental option which is off by default. The return value must be a defined, blessed objected that ISA Catalyst::View for this to work. Example:

    sub set_view_by_return :Chained(/) CaptureArgs(0) {
      my ($self, $c) = @_;
      return $c->view('CurrentView'); # $c->view  ISA 'MyApp::View::CurrentView'
    }

model_instance_from_state

Often you want to set your current model instance to 'any type of object'. The configuration "model_instance_from_return" expects the object to be something in the 'MyApp::Model' namespace. If this is not the case you can use this option.

    sub set_model_from_resultset :Chained CaptureArgs(1) {
      my ($self, $c, $id) = @_;
      return $c->model("Schema::User")->find($id);
    }

In this case the object returned is probably a 'MyApp::Schema::Result::User' so the option "model_instance_from_return" would not have worked.

AUTHOR

John Napiorkowski email:jjnapiork@cpan.org

SEE ALSO

Catalyst, Catalyst::Response

COPYRIGHT & LICENSE

Copyright 2017, John Napiorkowski email:jjnapiork@cpan.org

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