Catalyst Development
Building Web Applications Using Catalyst
Contents
MVC
Top BottomMVC is a design pattern keeping the three main areas of an application seperate, so that anyone can be replaced without affecting the other. The three areas are:
- Model: Schematic description of the data
- View: presentation layer
- Controller: program logic connecting data to view
Create Project
Top Bottomuse 'catalyst.pl MyApp' to initialise the application framework - creates MyApp directory and subdirectories required for the project
Start the server with script/myapp_server.pl. Options include:
- -r : restarts the server automatically when controllors change
- -d : starts the server in debug mode - displays lots of information on actions
The Application Class
Top Bottomlib/MyApp.pm
Contains the list of plugins/flags in use by the application. Defaults are:
- -Debug
- flag to enable debugging output
- Catalyst::Plugin::ConfigLoader
- provides automatic loader of configuration parameters
- Catalyst::Plugin::Static::Simple
- provides services for static content, eg CSS and images
Plenty of additional plugins are available to customise the application, including:
- Catalyst::Plugin::StackTrace
- sends errors to the browser
- Catalyst::Model::DBI
- access databases through traditional DBI interface
- Class::DBI
- traditional perl ORM engine, providing model objects for relational databases
- DBIx::Class
- preferred ORM engine
The Controller
Top BottomControllers contain methods that interact with user input. Initially the root controller Controller/Root.pm will handle requests for the top-level namespace in the application. Controller actions are subroutines, with additional attributes in their definition to specify which actions they handle. For example:
- sub index :Path :Args(0)
- handles actions where the requested path (specified using the :Path attribute) has zero arguments (specified using Arg(0)).
- sub default :Path
- responds to paths where no other controller action matches the path.
- sub end : ActionClass('RenderView')
- the 'end' action contains the code that runs after all other controller actions complete
Create new Controllers using
script/myapp_create.pl controller nameofcontroller
The View
Top BottomUse the Catalyst::Helper::View::TT or Catalyst::Helper::View::TTSite helper script to create the View:
script/myapp_create.pl view TT TTSite
Data from the controller are passed to the View templates for presentation of the results of an action. Views can be used to present data in web pages, pass data to a web-services application or to serve non-html files.
Connecting Controller to Views
Top BottomBy default controllers attempt to render views in the same namespace as the controller, with the same name as the action called. Thus the delete action in Controller::Robots will look for a template called root/robot/delete.tt. This default action can be over-ridden by setting explicitly setting the template in the action:
$c->stash(template) = 'other_view.tt';
The Model
Top Bottomuse the Catalyst::Helper::Model::DBIC::Schema helper script to create the model class:
script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB dbi:SQLite:myapp.db '' '' '{AutoCommit => 1}'where the first 'MyAppDB' is the name of the class to be created (in lib/MyApp/Model) and the second is the name of the schema file to be used to create the model (lib/MyAppDB.pm)
