With Catalyst you register your actions and address them directly. For example:
- sub hello : Absolute {
+ sub hello : Global {
my ( $self, $context ) = @_;
$context->response->output('Hello World!');
}
As illustrated earlier in our URL-to-Action dispatching example, the Context is always the second method parameter, behind the Component object reference or class name itself. Previously we called it C<$context> for clarity, but most Catalyst developers just call it C<$c>:
- sub hello : Absolute {
+ sub hello : Global {
my ( $self, $c ) = @_;
$c->res->output('Hello World!');
}
The last of these, the stash, is a universal hash for sharing data among application components. For an example, we return to our 'hello' action:
- sub hello : Absolute {
+ sub hello : Global {
my ( $self, $c ) = @_;
$c->stash->{message} = 'Hello World!';
$c->forward('show-message');
=item * B<Literal>
- sub bar : Absolute('foo/bar') { }
+ sub bar : Path('/foo/bar') { }
Matches only http://localhost:3000/foo/bar.
=item * B<Namespace-Prefixed>
package MyApp::Controller::My::Controller;
- sub foo : Relative { }
+ sub foo : Local { }
Matches http://localhost:3000/my/controller/foo. The action key must be prefixed with '?'.
But what if you also defined actions for /foo/boo and /foo/boo/hoo ?
- sub boo : Absolute('foo/boo') { .. }
- sub hoo : Absolute('foo/boo/hoo') { .. }
+ sub boo : Path('/foo/boo') { .. }
+ sub hoo : Path('/foo/boo/hoo') { .. }
Catalyst matches actions in most specific to least specific order:
Control the application flow with the C<forward> method, which accepts the key of an action to execute.
- sub hello : Absolute {
+ sub hello : Global {
my ( $self, $c ) = @_;
$c->stash->{message} = 'Hello World!';
$c->forward('check-message');
You can also forward to classes and methods.
- sub hello : Absolute {
+ sub hello : Global {
my ( $self, $c ) = @_;
$c->forward(qw/MyApp::M::Hello say_hello/);
}
- sub bye : Absolute {
+ sub bye : Global {
my ( $self, $c ) = @_;
$c->forward('MyApp::M::Hello');
}
This gives us a process() method and we can now just do $c->forward('MyApp::V::TT') to render our templates. The base class makes process() implicit, so we don't have to say C<$c-E<gt>forward(qw/MyApp::V::TT process/)>.
- sub hello : Absolute {
+ sub hello : Global {
my ( $self, $c ) = @_;
$c->stash->{template} = 'hello.tt';
}
$c->forward('MyApp::V::TT');
}
- sub view : Absolute {
+ sub view : Global {
my ( $self, $c, $id ) = @_;
$c->stash->{item} = MyApp::M::CDBI::Foo->retrieve($id);
}
package MyApp::C::Catalog;
- sub view : Relative { }
- sub list : Relative { }
+ sub view : Local { }
+ sub list : Local { }
package MyApp::C::Cart;
- sub add : Relative { }
- sub update : Relative { }
- sub order : Relative { }
+ sub add : Local { }
+ sub update : Local { }
+ sub order : Local { }
=head3 Testing