Last active
September 14, 2016 10:32
-
-
Save simonamor/1aa8245af3c549ee7de361487f361cec to your computer and use it in GitHub Desktop.
Working Catalyst Chained dispatch example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'