Skip to content

Instantly share code, notes, and snippets.

@simonamor
Last active September 14, 2016 10:32
Show Gist options
  • Select an option

  • Save simonamor/1aa8245af3c549ee7de361487f361cec to your computer and use it in GitHub Desktop.

Select an option

Save simonamor/1aa8245af3c549ee7de361487f361cec to your computer and use it in GitHub Desktop.
Working Catalyst Chained dispatch example
package MyApp::Controller::Root;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller' }
__PACKAGE__->config(namespace => '');
sub chain_root :Chained('/') :PathPart('') :CaptureArgs(0) {
my ($self,$c) = @_;
# chain root, just some pre-processing for templates performed here
$c->stash( here => 'using chain' );
}
sub index :Chained('chain_root') :PathPart('') :Args(0) {
my ($self,$c) = @_;
# action for /
$c->res->body('Index ' . $c->stash->{ here });
}
sub default :Chained('chain_root') :PathPart('') :Args {
my ($self,$c) = @_;
$c->response->body('Page not found ' . $c->stash->{ here });
$c->response->status(404);
}
sub test :Chained('chain_root') :PathPart('test') :Args(0) {
my ($self,$c) = @_;
# test page for /test
$c->res->body('Test ' . $c->stash->{ here });
}
sub end : ActionClass('RenderView') {}
__PACKAGE__->meta->make_immutable;
1;
@simonamor
Copy link
Author

simonamor commented Sep 14, 2016

catalyst.pl MyApp
replace MyApp/lib/MyApp/Controller/Root.pm with the above content
run server (MyApp/script/myapp_server.pl)
/ - it should say 'Index using chain'
/test - should say 'Test using chain'
/asdad - should say 'Page not found using chain'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment