Web applications with Catalyst

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    Web applications with Catalyst - Presentation Transcript

    1. Catalyst easy to use MVC framework Svilen Ivanov (svilen@netclime.com) 1 / 21 6/12/05
    2. What is MVC? Model View Controller: ● architecture that separates an application's data model, ■ user interface, and control logic into 3 distinct components modifications to one component can be made with minimal ■ impact to the others. Responsability ● model - domain-specific representation of the information ■ on which the application operates (database) view - renders the model into a form suitable for ■ interaction, typically a user interface (html) controller - responds to events and invokes changes on the ■ model and/or the view 2 / 21 6/12/05
    3. What is Catalyst? A framework for development of web application ● using Model-View-Controller pattern: Promotes the re-use of existing Perl modules that already ■ handle common web application Catalyst is controller ■ View: Template::Toolkit, Mason, HTML::Temlate, etc. ■ Model: Class::DBI, Tangram, Rose::DB, etc. ■ platform independent (really!) ■ Engines: CGI, FastCGI, mod_perl (1.3 and 2.0), self- ■ restartinng developement engine 3 / 21 6/12/05
    4. Why Catalyst? Doesn't aim to provide end to end solution (RnR) ● Flexability – use different components as M or V ● Reusability – plugins (extends runtime ● functionality): session, form validation, static files serving, unicode ■ Built-in developent tools: ● self-restarting developement web server on dev's machine ■ Distribute the application as CPAN module ● Framwork for auto and unit tests ● doen't need web server to generate pages ■ 4 / 21 6/12/05
    5. Catalyst Workflow 5 / 21 6/12/05
    6. Installing and running Installation from CPAN ● $ perl -MCPAN -e 'install Task::Catalyst' ■ Creating stub application ● $ catalyst.pl MyApp ■ # output omitted $ cd MyApp $ script/myapp_create.pl controller Library::Login Running ● $ script/myapp_server.pl ■ Open in browser: ● http://localhost:3000/library/login/ ■ 6 / 21 6/12/05
    7. Building application (1) Application Class – base class for your application ● package MyApp; use strict; use Catalyst qw/-Debug/; MyApp->config( name => 'My Application', # You can put anything else you want in here: my_configuration_variable => 'something', ); sub default : Private { my ( $self, $context ) = @_; $context->response->body('Catalyst rocks!'); } 7 / 21 6/12/05
    8. Actions Actions ● action is a subroutine usually executed when requesting ■ specific URL Action-to-URL mapping: ■ module: MyApp::Controller::Admin::Login url: http://localhost/admin/login The attribute of the subroutine defines the type of ● the action sub name : attributes { } example: sub bar : Regex('^item(d+)/order(d+)$') { } 8 / 21 6/12/05
    9. Types of actions (1) Literal ● package MyApp::Controller::My::Controller; sub bar : Path('foo/bar') { } # http://localhost:3000/my/controller/foo/bar sub bar : Path { } # http://localhost:3000/my/controller sub bar : Path('/foo/bar') { } # http://localhost:3000/foo/bar LocalRegex ● package MyApp::Controller::My::Controller; sub bar : LocalRegex('^widget(d+)$') { } # http://localhost:3000/my/controller/widget23 Regex ● sub bar : Regex('^item(d+)/order(d+)$') { } # http://localhost:3000/item23/order42 9 / 21 6/12/05
    10. Types of actions (2) Top-level ● package MyApp; sub foo : Global { } # http://localhost:3000/foo Namespace-Prefixed ● package MyApp::Controller::My::Controller; sub foo : Local { } # http://localhost:3000/my/controller/foo Private ● sub foo : Private { } Default actions ● default, index, begin, end ■ 10 / 21 6/12/05
    11. Context Context ● object passed to each action in order to interact w/ Catalyst ■ (get request parameters, set response body) sub hello : Global { my ( $self, $c ) = @_; $c->res->body('Hello World!'); } Important objects: ■ Request ($c->req) – web paranaters, cookies, headers, ▬ uploaded files ▬ Response ($c->res) – set document body, HTTP status code ▬ Configuration ($c->config) – static project configuration ▬ Log ($c->log) – print log messages ▬ Stash ($c->stash) – pass data from model to view 11 / 21 6/12/05
    12. Views - Template Toolkit Fast, powerful and extensible template processing ● system: rich presentation languageocumentation including tutorial ■ and reference manuals output filtering (for e.g. html escaping), exception handling ■ use directly hashes, arrays, objects in the template ■ templates are compiled to Perl code for maximum runtime ■ efficiency and performance. compiled templates are cached and can be written to disk ■ in "compiled form" to achieve cache persistance. documentation including tutorial and reference manuals ■ open source and free! :) ■ 12 / 21 6/12/05
    13. TT - Example t.pl my %vars = ( webpages => [ { url => 'http://foo.org', title => 'The Foo Organisation' } { url => 'http://bar.org', title => 'The Bar Organisation' } ]); $template->process(\%vars); template.tt: template.tt: [% INCLUDE header title = 'This is an HTML example' %] <h1>Some Interesting Links</h1> Links: <ul> [% FOREACH link = webpages %] <li><a href="[% link.url %]">[% link.title %]</a> href="[% %]">[% %]</a> [% END %] </ul> header.tt: <html><head><title>[% title %]</title></head> <html><head><title>[% %]</title></head> <body bgcolor="#ffffff"> 13 / 21 6/12/05
    14. Model - Rose::DB::Object Base class for objects that encapsulate a single ● row in a database table supports creating, loading, saving, deleting an object ■ fetching reffered by foreign-key objects ■ Rose::DB::Object::Manager ● Fetch multiple objects from the database using arbitrary ■ query conditions, limits, and offsets. Iterate over a list of object ■ Update/Delete objects that match a complex query. ■ Fetch objects along with "foreign objects" (related through ■ any of the supported relationship types) in a single query by automatically generating the appropriate SQL join(s). 14 / 21 6/12/05
    15. Rose – example (1) Defining object ● CREATE TABLE `products` ( `id` bigint(20) unsigned NOT NULL auto_increment, `name` varchar(255) NOT NULL default '', `price` decimal(10,2) NOT NULL default '0.00', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; package Product; use Rose::DB::Object; our @ISA = qw(Rose::DB::Object); __PACKAGE__->meta->table('products'); __PACKAGE__->meta->columns(qw(id name price)); __PACKAGE__->meta->primary_key_columns('id'); __PACKAGE__->meta->add_unique_key('name'); __PACKAGE__->meta->initialize; 15 / 21 6/12/05
    16. Rose – example (2) Operations using this object ● # add new product $p1 = Product->new(name => 'Bike'); $p1->save; # get product by its primary key $p2 = Product->new(id => '1'); $p2->load(); # access/modify fields print $p2->name; # prints 'Bike' $p2->name('ski'); # changes the name to 'ski' $p2->save(); # updates database; # deletes the object by unique key $p3 = Product->new(name => 'ski'); $p3->delete(); # the row is gone 16 / 21 6/12/05
    17. Rose – example (3) Work over set of objects using manager: ● package Product::Manager; use Rose::DB::Object::Manager; our @ISA = qw(Rose::DB::Object::Manager); sub object_class { 'Product' } __PACKAGE__->make_manager_methods('products'); Using the manager ● $products = Product::Manager->get_products(); foreach my $product (@$products) { print $product->name, " "; } $iterator = Product::Manager->get_products_iterator(); while($product = $iterator->next) { print $product->id, ' ', $product->name, " "; $iterator->finish if(...); # exit early? } print $iterator->total; # total iterated over 17 / 21 6/12/05
    18. Rose – example (4) Complex query ● $products = Product::Manager->get_products(query => [ name => { like => '%Hat' }, id => { ge => 7 }, or => [ price => 5.00, price => { lt => 10.00 }, ], ], sort_by => 'name', limit => 10, offset => 50); SELECT id, name, price FROM products WHERE name LIKE '%Hat' AND id >= 7 AND (price = 5.00 OR price < 10.00) ORDER BY name LIMIT 10 OFFSET 50 18 / 21 6/12/05
    19. Demonstration Starting development server ● Demonstrate basic actions ● protection from refresh ■ error handling ■ HTML escaping ■ Performance of the TT ■ Review ● model ■ view ■ controller ■ 19 / 21 6/12/05
    20. Conclusions Catalyst is rich web development platform ● it is AJAX-ready (a little marketing blurp :) ■ Using open source tools we can boost productivity ● let HTML developers work spearately on Template and ■ easily test the result let backend developers work on model classes, write ■ autotests Create better workflow ● the front- and back-end integration must be defined prior ■ start of development Avoid most common errors ● Building web applications can be fun! :) ● 20 / 21 6/12/05
    21. Thank you! Questions? ● 21 / 21 6/12/05

    + svilen.ivanovsvilen.ivanov, 3 years ago

    custom

    1386 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1386
      • 1386 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 46
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events