The System Administrator Notebook

Pages in the Catalyst section

First Steps Controllers Views Models Debugging Deployment CatManDat

Building Web Applications Using Catalyst

Page Contents


Top Bottom

MVC 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 used to render objects for the end user
  • Controller: program logic connecting user input to the view and model

Create Project

Top Bottom

use ' MyApp' to initialise the application framework - creates MyApp directory and subdirectories required for the project

Start the server with script/ Options include:

  • -r : restarts the server automatically when controllers change
  • -d : starts the server in debug mode - displays lots of information on actions
  • -p XXXX : used to specify a different TCP port to listen on from the default of 3000
  • --help : provides a full list of the options available

The Application Class

Top Bottom


Contains the list of plugins/flags in use by the application. Defaults are:

flag to enable debugging output
provides automatic loader of configuration parameters
provides services for static content, eg CSS and images

Plenty of additional plugins are available to customise the application, including:

sends errors to the browser
access databases through traditional DBI interface
traditional perl ORM engine, providing model objects for relational databases
preferred ORM engine

The Controller

Top Bottom

Controllers contain methods that interact with user input. Initially the root controller Controller/ 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/ controller nameofcontroller

The View

Top Bottom

Use the Catalyst::Helper::View::TT helper script to create the View:

script/ view HTML TT

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 Bottom

By 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/ This default action can be over-ridden by explicitly setting the template in the action:

$c->stash{template} = '';

The Model

Top Bottom

use the Catalyst::Helper::Model::DBIC::Schema helper script to create the model class:

script/ 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/