Catalyst & Chained
MVC
Bondage
Friday, February 6, 2009
Catalyst & Chained
Jay Shirley: ‘jshirley’
•Director, National Auto Sport Assoc.
•Business Director, Cold Hard Code
•Catalyst, DBIx::Class
•http://our.coldhardcode.com/jshirley/
•JAPH
Friday, February 6, 2009
Catalyst & Chained
What is
Catalyst?
Friday, February 6, 2009
Catalyst & Chained
What Catalyst
Is Not.
Friday, February 6, 2009
Catalyst & Chained
What Catalyst Is Not
•An experiment or prototype.
•A toy.
•Opinionated.
•An “all in one” package.
•Complicated.
Friday, February 6, 2009
Catalyst & Chained
What Catalyst Is
•Simple Framework.
•Trusts the developer.
•Buckets.
•Easily extensible.
Friday, February 6, 2009
Catalyst & Chained
The Basics
•MVC Oriented.
•Promotes good code design.
•Use all of CPAN, easily.
•Built-in server.
Friday, February 6, 2009
Catalyst & Chained
Dispatching
•Getting from here to there
•/public/url => /private/action
•Types:
•Chained
•Local
•Regular Expressions
Friday, February 6, 2009
Catalyst & Chained
What is
Chained?
Friday, February 6, 2009
Catalyst & Chained
Chained in 4 Sentences
•Defined execution path
•“Route” of methods to take
•Configurable URLs
•Spans Multiple Controllers
Friday, February 6, 2009
Catalyst & Chained
In Code:
sub base { }
sub after_base { }
sub after_after_base { }
Friday, February 6, 2009
Catalyst & Chained: 999 Words
In Pictures (and code)
A B display
Private Path: /a/b/end
sub A : Chained(‘/’) CaptureArgs(0){}
sub B : Chained(‘A’) CaptureArgs(0){}
sub display : Chained(‘B’) Args(0){}
Friday, February 6, 2009
Catalyst & Chained
What’s the point?
•Public URI != Internal Actions
•Easily add “automatic” code
(no more sub auto { })
•Each step is executed, in order.
•Can capture URI arguments
Friday, February 6, 2009
Catalyst & Chained: If you know mst, you know it isn’t a...
Free Lunch
•Short-circuit any request at any
point.
•Easily add midpoint actions.
•Descend namespaces.
•Sane and programmatic URI
construction.
•Easy abstraction.
Friday, February 6, 2009
Catalyst & Chained
Benefits
Friday, February 6, 2009
Catalyst & Chained
Configurable URLs
•Public Paths:
PathPart(‘public_url_part’)
•Internal Action:
sub action_name : ... { }
•Easily modifiable public paths to change
URL structure.
Friday, February 6, 2009
Catalyst & Chained: Your own personal Voltron
Configurable Paths
__PACKAGE__->config(
action => {
‘action_name’ => {
Chained => ‘/’,
PathPart(‘public’)
}
}
); # Public URL: /public
Friday, February 6, 2009
Catalyst & Chained
Why?
•Public URLs are your API
•Being able to change them is good.
•Not breaking them is better.
Friday, February 6, 2009
Catalyst & Chained
Abstraction
package MyApp::ControllerBase::Foo;
Friday, February 6, 2009
Catalyst & Chained: Pass the cool whip
Thinner Controllers
package MyApp::Controller::Bar;
use parent ‘MyApp::ControllerBase::Foo’;
sub setup : Chained(‘.’) CaptureArgs(0) {
# Setup stash, done!
}
Friday, February 6, 2009
Catalyst & Chained: Pass the cool whip
Thinner Controllers
package MyApp::Controller::Bar;
use parent ‘MyApp::ControllerBase::Foo’;
sub setup : Chained(‘.’) CaptureArgs(0) {
# Setup stash, done!
}
Friday, February 6, 2009
Catalyst & Chained
Common End-Points
package MyApp::ControllerBase::Foo;
use parent ‘Catalyst::Controller’;
sub display : Chained(‘...’) Args(0) {
}
Friday, February 6, 2009
Catalyst & Chained
Better
Applications
Friday, February 6, 2009
Catalyst & Chained
A Simple Example
•Photos
•Belong to a ‘person’
•Have tags
Friday, February 6, 2009
Catalyst & Chained
URL Structure
•Keep it simple:
•/person/$name/photos
•/tag/$name/photos
Friday, February 6, 2009
Catalyst & Chained
The Base Class
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub display : Chained(‘setup’)
PathPart(‘photos’) Args(0)
{
}
Friday, February 6, 2009
Catalyst & Chained
The Base Class
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub display : Chained(‘setup’)
PathPart(‘photos’) Args(0)
{
}
Friday, February 6, 2009
Catalyst & Chained
The Base Class
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub display : Chained(‘setup’)
PathPart(‘photos’) Args(0)
{
}
Friday, February 6, 2009
Catalyst & Chained
Person::Photos
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
sub setup : Chained(‘.’) PathPart(‘’)
CaptureArgs(0)
{
my ( $self, $c ) = @_;
$c->stash->{photos} =
$c->stash->{person}->photos;
}
Friday, February 6, 2009
Catalyst & Chained
Person::Photos
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
sub setup : Chained(‘.’) PathPart(‘’)
CaptureArgs(0)
{
my ( $self, $c ) = @_;
$c->stash->{photos} =
$c->stash->{person}->photos;
}
Friday, February 6, 2009
Catalyst & Chained
Person::Photos
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
sub setup : Chained(‘.’) PathPart(‘’)
CaptureArgs(0)
{
my ( $self, $c ) = @_;
$c->stash->{photos} =
$c->stash->{person}->photos;
}
Friday, February 6, 2009
Catalyst & Chained
A Note
•Person::Photos only:
•cares about itself.
•is very thin (just the stash, ma’am).
•can actually be abstracted to just
configuration
Friday, February 6, 2009
Catalyst & Chained
Tag::Photos
•Same as Person::Photos, except:
•$c->stash->{tag} instead of
$c->stash->{person}
•has a different template.
Friday, February 6, 2009
Catalyst & Chained
The Base Class, Version 2
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub setup : Chained(‘.’) PathPart(‘’) CaptureArgs(0) {
my ( $self, $c ) = @_;
my $stash = $self->{stash_key};
my $source = $self->{source_key};
$c->stash->{$stash} = $c->stash->{$source}->photos;
}
sub display ... { # unchanged }
Friday, February 6, 2009
Catalyst & Chained
Config: Person
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
__PACKAGE__->config(
source_key => ‘person’,
stash_key => ‘photos’
);
Friday, February 6, 2009
Catalyst & Chained
Config: Tag
package MyApp::Controller::Tag::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
__PACKAGE__->config(
source_key => ‘tag’,
stash_key => ‘photos’
);
Friday, February 6, 2009
Catalyst & Chained
Config: Tag
package MyApp::Controller::Tag::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
__PACKAGE__->config(
source_key => ‘tag’,
stash_key => ‘photos’
);
Friday, February 6, 2009
Catalyst & Chained
That’s it
•Now, you just work with:
•/person/photos/display.tt
•/tag/photos/display.tt
•Thin controllers, how to get photos
determined just from configuration.
Friday, February 6, 2009
Catalyst & Chained
Questions?
http://github.com/jshirley/catalystx-example-chained/tree
Friday, February 6, 2009
1 comments
Comments 1 - 1 of 1 previous next Post a comment