You are on page 1of 22

INTRODUCTION TO CATALYST

Object oriented Perl MVC Framework

By BUNTY RAY

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.  Responsibility 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

mod_perl (1.  Model: Class::DBI. HTML::Template. etc.3 and 2.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. FastCGI. Rose::DB. platform independent (really!)  Engines: CGI.0). etc. Tangram. self restarting development engine  .

unicode ●Built-in development tools: Self-restarting development web server on dev's machine ● Distribute the application as CPAN module ● Framework for auto and unit tests Doesn't need web server to generate pages . static files serving.WHY CATALYST?         Doesn't aim to provide end to end solution (RnR) Flexibility – use different components as M or V Reusability – plugins (extends runtime functionality): session. form validation.

CATALYST ARCHITECTURE FLOW .

pl (Before proceeding please check the next slide to confirm the output)  ● Creating stub application  $ catalyst.INSTALLING AND RUNNING Installation from CPAN  $ perl -MCPAN -e 'install Task::Catalyst‘  Confirm successful installation – catalyst.pl  ● Open in browser:  http://localhost:3000/library/login/  C:\Users\BUNTY\ Desktop\Server-Started-Out .pl controller Library::Login  ● Running  $ script/myapp_server.pl MyApp  # output omitted [Next Slide]  $ cd MyApp  $ script/myapp_create.

  Options: -force don't create a . and allows you to upgrade the skeleton of your old application.   application-name must be a valid Perl module name and can include "::".PL -scripts only update helper scripts -short use short names.pl' creates a skeleton for a new application.pl -force -scripts MyApp .     Examples: catalyst.pl [options] application-name   'catalyst.pl MyApp   To upgrade your app to a new version of Catalyst: catalyst. M/V/C instead of Model/View/Controller.new file where a file to be created exists -help display this help and exit -makefile only update Makefile.pl My::App catalyst. which will be converted to '-' in the project name. ensure you see the bellow output – developer@dev01-th:~$ catalyst.CHECK IF INSTALLED PROPERLY  When you execute catalyst.pl .pl Usage: catalyst.

CATALYST URL .

conf" created "MyApp/lib/MyApp.pm“ created "MyApp/lib/MyApp/Controller/Root.pl MyApp created "MyApp" created "MyApp/script" created "MyApp/lib" created "MyApp/root" created "MyApp/root/static“ created "MyApp/root/static/images" created "MyApp/t" created "MyApp/lib/MyApp" created "MyApp/lib/MyApp/Model" created "MyApp/lib/MyApp/View" created "MyApp/lib/MyApp/Controller" created "MyApp/myapp.pm" created "MyApp/README" created "MyApp/Changes" created "MyApp/t/01app.t" .t" created "MyApp/t/02pod.DIRECTORIES CREATED UNDER STUB APPS developer@dev01-th:~$ catalyst.

png" created "MyApp/root/static/images/btn_120x50_powered_shadow.png" created "MyApp/root/static/images/btn_88x31_powered_shadow.png" created "MyApp/root/static/images/btn_88x31_powered.ico“ created "MyApp/Makefile.png" created "MyApp/root/static/images/btn_120x50_built.pl" created "MyApp/script/myapp_server.png" created "MyApp/root/static/images/btn_88x31_built.png" created "MyApp/root/static/images/btn_120x50_built_shadow.pl" … .PL" created "MyApp/script/myapp_cgi.CONTINUED created "MyApp/t/03podcoverage.pl" created "MyApp/script/myapp_fastcgi.png" created "MyApp/root/static/images/btn_120x50_powered.png" created "MyApp/root/static/images/btn_88x31_built_shadow.png" created "MyApp/root/favicon.t" created "MyApp/root/static/images/catalyst_logo.pl" created "MyApp/script/myapp_test.pl" created "MyApp/script/myapp_create.

1 developer developer 0 2010-08-22 01:54 pm_to_blib drwxr-xr-x 8 developer developer 4096 2010-08-22 01:54 blib -rw-r--r-.WALK THROUGH THE “MYAPP” DIRECTORY drwxr-xr-x 2 developer developer 4096 2010-08-22 01:06 script -rw-r--r-.conf -rw-r--r-.1 developer developer 488 2010-08-22 01:53 META.1 developer developer 133 2010-08-22 01:06 myapp.yml -rw-r--r-.1 developer developer 26474 2010-08-22 01:53 Makefile -rw-r--r-.1 developer developer 144 2010-08-22 01:06 Changes drwxr-xr-x 3 developer developer 4096 2010-08-22 01:20 lib drwxr-xr-x 4 developer developer 4096 2010-08-22 01:49 root drwxr-xr-x 4 developer developer 4096 2010-08-22 01:53 inc -rw-r--r-.1 developer developer 52 2010-08-22 01:06 README -rw-r--r-.1 developer developer 624 2010-08-22 01:55 Makefile.PL drwxr-xr-x 2 developer developer 4096 2010-08-22 02:19 t .

This can be used as an help for end user or other developers. Changes – This is simply a text file where you can document changes to your application. myapp.conf – This is the main configuration file for the application which is loaded when your application starts.P It also contains a list of your applications CPAN dependencies and automatically install them. .PL – This script generates a Makefile to build . R EADME – It’s a text with information on your application.pl MyApp” (output attached) Desktop\Running_MakeFile.…CONTINUED Makefile.Pl) after you executed the command C:\Users\BUNTY\ “catalyst. This file is invoked (perl Makefile. test and install your application.

which defines the namespace and inheritance that are necessary to make this Catalyst application. This is where the code that generates the welcome page is located.pl.g.pm ” .pm.…CONTINUED  /lib Directory It’s the hear of the application. /lib directory contains 3 key directories – Controller . This directory contains a file MyApp. This will handle all root level ( / ) URL’s. View and Model Catalyst loads the Controller. It also contains list of plugins to load application specific configuration. MyApp under /lib is like a namespace of a module. catalyst. Basically the default Controller is “ Root. e. The application is created with whatever argument to catalyst.pl MyCompany::HR::Timeslips This will create the application with namespace MyCompany::HR::Timeslips and in file system its will be /lib/MyCompany/HR/Timeslips . Model and View from these directories.

Contains a /static sub directory which holds static contents like image files and stylesheet. css files etc. (iii) myapp_fastcgi. Its very slow and low performer. test and modify your application – (i) myapp_server. /script Directory Contains scripts needed to run. (ii) myapp_cgi. so should be used as the last option.pl -> Allows you to run your application in a FastCGI server.pl -> This is the development server (a self-contained HTTPD server) that you can use to run your application while you are developing it.CONTINUED … /root Directory Contains the template and other non code support file.(Production Mode) .pl -> This is a script for deploying your application with a web server that can not use mod_perl or FastCGI.

t.pl -> Basically used to test an action without opening browser. after Makefile.pl for your application. is valid. 02pod. (v) myapp_create.t.PL creates the Makefile.… CONTINUED (iv) myapp_test. This is customized version of the catalyst. . inside your application. It can be used to create Controller.t. 01app. /t Directory The t directory is where your application's automatic tests are stored. 03podcoverage. You can run the tests in this command by running make test.pl -> This is a helper script. which will pass if your Plain Old Documentation (POD) embedded API documentation. which is a test that passes if your application compiles. Model and View. which tests that every public function in your application has some documentation.

http://url/controller.html Will match the hello Controller (Hello.g.pm e.132:3000/hello/index.pm .HANDLING URL REQUEST When a user makes a request using Browser. http://192.pm) within the index will match the subroutine Hello.action (hello) (test) /lib/appname/Controller/Hello. Catalyst looks for the appropriate method that can handle the request within package called Controller.168.187.

=head1 NAME MyApp::Controller::Hello .CREATING A CONTROLLER perl script/myapp_create.Catalyst Controller =head1 DESCRIPTION Catalyst Controller. =head1 METHODS =cut =head2 index =cut C:\Users\BUNTY\ Desktop\Creating A control . use strict. use warnings. use parent 'Catalyst::Controller'.pm package MyApp::Controller::Hello.pl controller Hello This will create /lib/MyApp/Controller/Hello.

$c->response->body('Matched MyApp::Controller::Hello in Hello. BEGIN {extends 'Catalyst::Controller'. and then some use statements (to tell Perl that this module uses Moose).'). . there is some POD (the text starting with =) that you can fill in to provide some API documentation (see man perlpod for the syntax.. Then.. At the top is the package declaration (so Perl knows what this module is named). } =head1 AUTHOR developer.CONTINUED … sub index :Path :Args(0) { my ( $self. } to tell Moose/Perl that this module is a Catalyst Controller. =head1 LICENSE This library is free software. $c ) = @_. you can redistribute it and/or modify it under the same terms as Perl itself. Next. =cut 1.

| Path | Private | +-------------------------------------+--------------------------------------+ | / | /index | | / | /default | | /hello | /hello/index | Opening the URL http://localhost:3000/hello will show you the following: Matched MyApp::Controller::Hello in Hello .pl [debug] Loaded Path actions: .… CONTINUED Let us check what just happened by running the server with the following command: perl script/myapp_server. -------------------------------------+--------------------------------------.

Technically this means we can create as many views of Catalyst::View:TT with different names. Currently we will make use of Template Toolkit templating system to enable TT view for our HTML page.pm Controller under /lib/MyApp/Controller location from the subroutine index().pm (1st TT) based on the standard Catalyst::View::TT (2nd TT). Step 2: Now create the view – C:\Users\BUNTY\ Desktop\Creating a TT_View perl script/myapp_create. . View is a system that defines how content can be rendered.pl view TT TT This means create a view called View/TT. Its time now to show some HTML content using View. $c ->response ->body() Catalyst knows how to take care of forwarding the response to the default view after the Controllers execution.ADDING A VIEW Now the URL is mapped to Controller’s method. (Template Toolkit module is available with Catalyst) Step 1: Remove the below line from Hello.

PL (b) make C:\Users\BUNTY\ Desktop\Running_make_After Step 4: Now create a template under /root/hello directory with a name index.P (a) perl Makefile. but is advised to do since it will be useful when you are migrating the application to a different machine. This step is not mandatory. Now run the below commands – C:\Users\BUNTY\ Desktop\Running_MakeFile.Pl with the below content under “requires” section. Now add content to the . world! HTML Document .tt file to display hello .tt /root is the default location from where Catalyst::View::TT will look for templates.… CONTINUED Step 3: Now update the Makefile. requires ‘Catalyst::View::TT’ =>0.

tt" [info] Request took 0.168.675/s) .SERVER OUTPUT ON HITTING – HTTPS://192.005531s | '----------------------------------------------------------------+---------- .1" [debug] Path is "hello" [debug] Rendering template "hello/index.----------------------------------------------------------------+-----------.239/s) .168.187.000436s | | /end | 0.187.7014 You can connect to your server at http://dev01-th:3000 [info] *** Request 1 (0.187.007426s | | -> MyApp::View::TT->process | 0.168.tt" [info] Request took 0.----------------------------------------------------------------+-----------.021169s (47. | Action | Time | +----------------------------------------------------------------+-----------+ | /hello/index | 0.077/s) [7185] [Mon Aug 23 00:33:51 2010] *** [debug] "GET" request for "hello" from "192.1" [debug] Path is "hello" [debug] Rendering template "hello/index.197455s | '----------------------------------------------------------------+-----------' [info] *** Request 2 (0.213881s (4.000607s | | /end | 0. | Action | Time | +----------------------------------------------------------------+-----------+ | /hello/index | 0.201312s | | -> MyApp::View::TT->process | 0.132:3000/HELLO [info] MyApp powered by Catalyst 5.001/s) [7185] [Mon Aug 23 01:13:47 2010] *** [debug] "GET" request for "hello" from "192.