=head1 NAME Catalyst::Manual::Tutorial::CatalystBasics - Catalyst Tutorial - Part 2: Catalyst Application Development Basics =head1 OVERVIEW This is B for the Catalyst tutorial. L =over 4 =item 1 L =item 2 B =item 3 L =item 4 L =item 5 L =item 6 L =item 7 L =item 8 L =item 9 L =back =head1 DESCRIPTION In this part of the tutorial, we will create a very basic Catalyst web application. Though simple in many respects, this section will already demonstrate a number of powerful capabilities such as: =over 4 =item * Helper Scripts Catalyst helper scripts that can be used to rapidly bootstrap the skeletal structure of an application. =item * MVC Model/View/Controller (MVC) provides an architecture that facilitates a clean "separation of control" between the different portions of your application. Given that many other documents cover this subject in detail, MVC will not be discussed in depth here (for an excellent introduction to MVC and general Catalyst concepts, please see L. In short: =over 4 =item * Model The model usually represents a data store. In most applications, the model equates to the objects that are created from and saved to your SQL database. =item * View The view takes model objects and renders them into something for the end user to look at. Normally this involves a template-generation tool that creates HTML for the user's web browser, but it could easily be code that generates other forms such as PDF documents, e-mails, or Excel spreadsheets. =item * Controller As suggested by its name, the controller takes user requests and routes them to the necessary model and view. =back =item * ORM The use of Object-Relational Mapping (ORM) technology for database access. Specifically, ORM provides an automated and standardized means to persist and restore objects to/from a relational database. =back B: Note that all of the code for this part of the tutorial can be pulled from the Catalyst Subversion repository in one step with the following command: svn co http://dev.catalyst.perl.org/repos/Catalyst/tags/examples/Tutorial/MyApp/5.7/CatalystBasics MyApp =head1 CREATE A CATALYST PROJECT Catalyst provides a number of helper scripts that can be used to quickly flesh out the basic structure of your application. All Catalyst projects begin with the C helper. In the case of this tutorial, use the Catalyst C script to initialize the framework for an application called C: $ catalyst.pl MyApp created "MyApp" created "MyApp/script" created "MyApp/lib" created "MyApp/root" ... created "MyApp/script/myapp_create.pl" $ cd MyApp The C helper script will display the names of the directories and files it creates. Though it's too early for any significant celebration, we already have a functioning application. Run the following command to run this application with the built-in development web server: $ script/myapp_server.pl [debug] Debug messages enabled [debug] Loaded plugins: .----------------------------------------------------------------------------. | Catalyst::Plugin::ConfigLoader 0.06 | | Catalyst::Plugin::Static::Simple 0.14 | '----------------------------------------------------------------------------' [debug] Loaded dispatcher "Catalyst::Dispatcher" [debug] Loaded engine "Catalyst::Engine::HTTP" [debug] Found home "/root/dev/MyApp" [debug] Loaded components: .-----------------------------------------------------------------+----------. | Class | Type | +-----------------------------------------------------------------+----------+ | MyApp::Controller::Root | instance | '-----------------------------------------------------------------+----------' [debug] Loaded Private actions: .----------------------+--------------------------------------+--------------. | Private | Class | Method | +----------------------+--------------------------------------+--------------+ | /default | MyApp::Controller::Root | default | | /end | MyApp::Controller::Root | end | '----------------------+--------------------------------------+--------------' [info] MyApp powered by Catalyst 5.7000 You can connect to your server at http://localhost.localdomain:3000 Point your web browser to L (substituting a different hostname or IP address as appropriate) and you should be greeted by the Catalyst welcome screen. Information similar to the following should be appended to the logging output of the development server: [info] *** Request 1 (0.043/s) [6003] [Fri Jul 7 13:32:53 2006] *** [debug] "GET" request for "/" from "127.0.0.1" [info] Request took 0.067675s (14.777/s) .----------------------------------------------------------------+-----------. | Action | Time | +----------------------------------------------------------------+-----------+ | /default | 0.002844s | | /end | 0.000207s | '----------------------------------------------------------------+-----------' Press Ctrl-C to break out of the development server. =head1 CREATE A SQLITE DATABASE In this step, we make a text file with the required SQL commands to create a database table and load some sample data. Open C in your editor and enter: -- -- Create a very simple database to hold book and author information -- CREATE TABLE books ( id INTEGER PRIMARY KEY, title TEXT , rating INTEGER ); -- 'book_authors' is a many-to-many join table between books & authors CREATE TABLE book_authors ( book_id INTEGER, author_id INTEGER, PRIMARY KEY (book_id, author_id) ); CREATE TABLE authors ( id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT ); --- --- Load some sample data --- INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5); INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5); INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4); INSERT INTO books VALUES (4, 'Perl Cookbook', 5); INSERT INTO books VALUES (5, 'Designing with Web Standards', 5); INSERT INTO authors VALUES (1, 'Greg', 'Bastien'); INSERT INTO authors VALUES (2, 'Sara', 'Nasseh'); INSERT INTO authors VALUES (3, 'Christian', 'Degu'); INSERT INTO authors VALUES (4, 'Richard', 'Stevens'); INSERT INTO authors VALUES (5, 'Douglas', 'Comer'); INSERT INTO authors VALUES (6, 'Tom', 'Christiansen'); INSERT INTO authors VALUES (7, ' Nathan', 'Torkington'); INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman'); INSERT INTO book_authors VALUES (1, 1); INSERT INTO book_authors VALUES (1, 2); INSERT INTO book_authors VALUES (1, 3); INSERT INTO book_authors VALUES (2, 4); INSERT INTO book_authors VALUES (3, 5); INSERT INTO book_authors VALUES (4, 6); INSERT INTO book_authors VALUES (4, 7); INSERT INTO book_authors VALUES (5, 8); B: See Appendix 1 for tips on removing the leading spaces when cutting and pasting example code from POD-based documents. Then use the following command to build a C SQLite database: $ sqlite3 myapp.db < myapp01.sql If you need to create the database more than once, you probably want to issue the C command to delete the database before you use the C command. Once the C database file has been created and initialized, you can use the SQLite command line environment to do a quick dump of the database contents: $ sqlite3 myapp.db SQLite version 3.2.2 Enter ".help" for instructions sqlite> select * from books; 1|CCSP SNRS Exam Certification Guide|5 2|TCP/IP Illustrated, Volume 1|5 3|Internetworking with TCP/IP Vol.1|4 4|Perl Cookbook|5 5|Designing with Web Standards|5 sqlite> .q $ Or: $ sqlite3 myapp.db "select * from books" 1|CCSP SNRS Exam Certification Guide|5 2|TCP/IP Illustrated, Volume 1|5 3|Internetworking with TCP/IP Vol.1|4 4|Perl Cookbook|5 5|Designing with Web Standards|5 As with most other SQL tools, if you are using the full "interactive" environment you need to terminate your SQL commands with a ";" (it's not required if you do a single SQL statement on the command line). Use ".q" to exit from SQLite from the SQLite interactive mode and return to your OS command prompt. =head1 EDIT THE LIST OF CATALYST PLUGINS One of the greatest benefits of Catalyst is that it has such a large library of plugins available. Plugins are used to seamlessly integrate existing Perl modules into the overall Catalyst framework. In general, they do this by adding additional methods to the C object (generally written as C<$c>) that Catalyst passes to every component throughout the framework. By default, Catalyst enables three plugins/flags: =over 4 =item * C<-Debug> Flag Enables the Catalyst debug output you saw when we started the C