200 likes | 368 Views
An Introduction to Catalyst YAPC::EU 2005 Braga, Portugal Marcus Ramberg, The Cabal. Catalyst in 40 minutes. About me. Name: Marcus Ramberg A/S/L: 28/No thanks, I'm married/Oslo,Norway Co-maintainer of Catalyst mramberg@cpan.org Oslo Perlmonger Working for ABC Startsiden
E N D
An Introduction to Catalyst YAPC::EU 2005 Braga, Portugal Marcus Ramberg, The Cabal Catalyst in 40 minutes
About me • Name: Marcus Ramberg • A/S/L: 28/No thanks, I'm married/Oslo,Norway • Co-maintainer of Catalyst • mramberg@cpan.org • Oslo Perlmonger • Working for ABC Startsiden • draven @ magnet :-)
Introduction to Catalyst. Overview Features Ajax autocompletion screencast Scaffolding screencast From scaffolding to app – BookDB Conclusion Overview • What is Catalyst? • Catalyst is a new MVC framework for Perl. Catalyst borrows from other frameworks, such as Ruby on Rails and Apache Struts, but its main goal is to be a flexible, powerful, and fast framework for developing any type of web project in Perl.
Some Numbers • 10 Engines (Apache,FastCGI,Standalone +++) • 8 models (CDBI,Tangram,DBIC, Xapian +++) • 6 supported views ( TT,Mason,H-T,Petal +++) • 48 plugins in trunk (Session, Forms, Cache +++) • 16,539 Total Physical Source Lines of Code (Catalyst trunk) • 445 tests • 1077 committed revisions • 50% chance Gabbana is drunk
The Context object • traditionally named $c • Instance of your main application • Subclass of the Catalyst application • All plugins are part of the context through multiple inheritance • Passed around with the forwarding mechanism to controllers and views • Provides access to Request and Response objects as well as the Stash.
Request / Response • Platform independent API • Engines populate Request before dispatching, and parse Response after the actions • Provides unified access to request params - • request->param('name') • Provides a simple access for HTTP Uploads • Using HTTP::Headers for request and response headers • Currently using scalars. Next release will support IO for uploads and serving large content.
The Stash • “An universal hash” • $c->stash->{item}=MyApp::M::Item->retrieve($item_id); • Request lifetime • How to communicate between actions, and with templates • Variables in stash are automatically-exposed to views.
The Dispatcher • Dispatches to actions based on URI path • Actions defined as attributes: • sub hello : Local {} • All actions listed at startup • Matching private action namespace for forwarding. • c->forward('/<controller>/<action>'); • works like a eval cage for a function call • continues processing! If that`s not what you want, use detach • pass arguments with a hashref • Redispatch within your application? • Catalyst::Plugin::SubRequest
The Actions • action types: • : Global – matches /<action>, no matter what controller • : Local – controller sensitive. /<controller>/<action> • : Path – Matches specified path; sub test : Path('/t/t/test') • : Regex – regular expressions • : Private – not available from URL – only for forwarding • Builtin: • default - fallback • auto – allows you to stop dispatching – return 0; • begin – before dispatching – can be overridden in Controllers • end – after dispatching – Typically forward to view.
Development Environment • Code Generation • Standalone Server • Debug screen • Extendable Logging integration • Catalyst::Log::Dispatch • Catalyst::Log::Color • Full API documentation
Deployment • Engines provide platform independence • Apache (ModPerl 1/1.9/2) • IIS (FastCGI) • Zeus • POE • SpeedyCGI • Standalone Server • Helper makes framework for installation/testing • Uses standard CPAN deployment mechanisms • Auto-detection of Application root • Easily use external configuration files.
Auto complete screencast • Making a simple Catalyst application • Like “Google Suggest” • AJAX Powered • Using Catalyst::Plugin::Prototype • Wrapping HTML::Prototype • A port of the Rails javascript helpers. • Now also including script.aculo.us effects, draganddrop and controls library. • scripts/myapp_create.pl Prototype to generate javascript files for inclusion in your app.
Scaffolding • Using the Catalyst Helper API • scripts/myapp_create.pl controller <name> Scaffolding <dbi::class> • Simple scaffolding. Low on magic, high on carbohydrates. • Build your app around the Scaffolding, not the other way around. • Use as much or as little as makes sense. • Lets watch the Screencast
BookDB • From Scaffolding to application • Maypole CRUD application I built in january • So let us take the bookdb.sql and see what we can do. • First – build the skeleton • catalyst.pl BookDB • Make the db • sqlite3 book.db <../bookdb.sql • And then the model • script/bookdb_create.pl model BookDB CDBI dbi:sqlite:book.db
BookDB • Create the view: • script/bookdb_create.pl view TT TT • Now let's try to get the scaffolding up: • script/bookdb_create.pl controller Book Scaffold BookDB::Book • Add a few plugins to Bookdb.pm: • use Catalyst qw/-Debug FormValidator FillInForm DefaultEnd/; • And while we are there, let's change Default: • $c->forward('/book/default'); • Finally, add a couple of required CDBI plugins: • additional_base_classes => [qw/Class::DBI::AsForm Class::DBI::FromForm/],
BookDB • All right, scaffolding is up and running, now what? • Those columns are a bit messy. • __PACKAGE__->columns(list=>qw/title author publisher year/); • [% FOR column = table_class.columns('list') %] • Want fancy labels? Write them. • Validation? Change the DFV profile: • required => [qw /title author/], • constraints => {year => qr/^\d{4}$/}
Adding books from Amazon • Thanks to Simon Cozens for this trick. • Adding basic support for checking out books • At this point we hand it over to Gabb! • Code available in Catalyst trunk • http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/BookDB
Handel Demo • Handel is a quick and not-so-dirty ecommerce framework with AxKit taglib support and TT2 (Template Toolkit) support. It was started for the conversion of an IIS/ASP based commerce site to Apache/ModPerl, but CLACO decided to release it to CPAN. Handel also works well with Catalyst: • catalyst.pl MyApp • sqlite < handel.sql • myapp_create.pl Handel::Scaffold dbi:SQLite:dbname=/tmp/handel.db • http:///localhost:3000/cart/ • Helpers soon to be released
Questions? • Q: So Catalyst looks good, but I heard it was part of a secret plan by The Cabal to undermine CPAN by flooding it with Catalyst modules? • A: There is no Cabal. • Others? No? Really? Ok, if you have a question later: • mailinglist: catalyst-subscribe@lists.rawmode.org • irc #catalyst @ irc.perl.org • My phone number: • Only for hot chicks ;-)