1Catalyst::Manual::TutorUisaelr::C0o1n_tIrnitbruot(e3d)PCeartlalDyosctu:m:eMnatnautailo:n:Tutorial::01_Intro(3)
2
3
4
6 Catalyst::Manual::Tutorial::01_Intro - Catalyst Tutorial - Chapter 1:
7 Introduction
8
10 This is Chapter 1 of 10 for the Catalyst tutorial.
11
12 Tutorial Overview
13
14 1. 01_Introduction
15
16 2. Catalyst Basics
17
18 3. More Catalyst Basics
19
20 4. Basic CRUD
21
22 5. Authentication
23
24 6. Authorization
25
26 7. Debugging
27
28 8. Testing
29
30 9. Advanced CRUD
31
32 10. Appendices
33
35 This tutorial provides a multi-part introduction to the Catalyst Web
36 Framework. It seeks to provide a rapid overview of many of its most
37 commonly used features. The focus is on the real-world best practices
38 required in the construction of nearly all Catalyst applications.
39
40 Although the primary target of the tutorial is users new to the
41 Catalyst framework, experienced users may wish to review specific
42 sections (for example, how to use DBIC for their model classes, how to
43 add authentication and authorization to an existing application, and/or
44 form management).
45
46 The most recent code for the tutorial is included on the Tutorial
47 Virtual Machine you can download from:
48
49 <http://cattut.shadowcat.co.uk/>
50
51 See "STARTING WITH THE TUTORIAL VIRTUAL MACHINE" below for instructions
52 getting and using the VM.
53
54 Should you wish to download the code directly, you get pull it via the
55 following command (note: will probably be switching to git soon):
56
57 svn co http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/ CatalystTutorial
58
59 This will download the most recent code for each chapter of the
60 tutorial into the CatalystTutorial directory on your machine.
61
62 These reference implementations are provided so that when you follow
63 the tutorial, you can use the code to ensure that your system is set up
64 correctly (which shouldn't be an issue if you use the Tutorial Virtual
65 Machine), :-) and that you have not inadvertently made any typographic
66 errors, or accidentally skipped part of the tutorial.
67
68 NOTE: You can use any Perl-supported OS and environment to run
69 Catalyst. It should make little or no difference to Catalyst's
70 operation, but this tutorial has been written using the Debian-based
71 Tutorial Virtual Machine that you can download and use to work through
72 the full tutorial step by step. WE STRONGLY RECOMMEND THAT YOU USE THE
73 VIRTUAL MACHINE IMAGE TO WORK THROUGH THE TUTORIAL to avoid issues that
74 may crop up if you are working with a different configuration. We have
75 tested the Tutorial Virtual Machine to make sure all of the examples
76 work correctly, but it is hard to guarantee this on other platforms and
77 versions.
78
79 If you would prefer to install directly from CPAN and not use the
80 Tutorial Virtual machine, you can download the example program and all
81 the necessary dependencies to your local machine by installing the
82 "Task::Catalyst::Tutorial" distribution:
83
84 cpan Task::Catalyst::Tutorial
85
86 This will also test to make sure the dependencies are working. If you
87 have trouble installing these, please ask for help on the #catalyst IRC
88 channel, or the Catalyst mailing list.
89
90 Subjects covered by the tutorial include:
91
92 · A simple application that lists and adds books.
93
94 · The use of DBIx::Class (DBIC) for the model (including some of the
95 more advanced techniques you will probably want to use in your
96 applications).
97
98 · How to write CRUD (Create, Read, Update, and Delete) operations in
99 Catalyst.
100
101 · Authentication ("auth").
102
103 · Role-based authorization ("authz").
104
105 · Attempts to provide an example showing current (5.9) Catalyst
106 practices.
107
108 · The use of Template Toolkit (TT).
109
110 · Useful techniques for troubleshooting and debugging Catalyst
111 applications.
112
113 · The use of SQLite as a database (with code also provided for MySQL
114 and PostgreSQL). (Note: Because we make use of the DBIx::Class
115 Object Relational Mapping [ORM] layer, out our application will be
116 database agnostic and can easily be used by any of the databases
117 supported by DBIx::Class.)
118
119 · The use of HTML::FormFu or HTML::FormHandler for automated form
120 processing and validation.
121
122 This tutorial makes the learning process its main priority. For
123 example, the level of comments in the code found here would likely be
124 considered excessive in a "normal project." Because of their
125 contextual value, this tutorial will generally favor inline comments
126 over a separate discussion in the text. It also deliberately tries to
127 demonstrate multiple approaches to various features (in general, you
128 should try to be as consistent as possible with your own production
129 code).
130
131 Furthermore, this tutorial tries to minimize the number of controllers,
132 models, TT templates, and database tables. Although this does result
133 in things being a bit contrived at times, the concepts should be
134 applicable to more complex environments. More complete and complicated
135 example applications can be found at
136 <http://wiki.catalystframework.org/wiki/resources/catalystexamples> and
137 in the "examples" area of the Catalyst Subversion repository at
138 <http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/>.
139
141 The steps below briefly outline how you can download the Tutorial
142 Virtual Machine. This document uses the term "host machine" to refer
143 to the physical machine where you will run the virtualization software
144 and boot up the VM. The terms "guest machine" or just "VM" refer to
145 the virtual machine itself -- the thing where you actually do the
146 tutorial (and that you boot up on the "host machine").
147
148 Note: Throughout the tutorial, we will shows the UNIX shell prompt as
149 ""$"". If you are using the Tutorial VM, the prompt will really be
150 ""catalyst@catalyst:~$"" (where ""~"" will change to show your current
151 directory), but we will keep it short and just use ""$"".
152
153 1. Download a Tutorial Virtual Machine image from
154 <http://cattut.shadowcat.co.uk/>
155
156 A big thanks to Shadowcat Systems for hosting the virtual machines
157 (and everything else they do for the Perl community)!
158
159 2. Uncompress the image on the "host machine":
160
161 MAINCOMPUTER:~$ tar zxvf CatalystTutorial.tgz
162
163 3. Boot the virtual machine using a tool like VMWare Player
164 <http://www.vmware.com/products/player> or VirtualBox
165 <http://www.virtualbox.org/>.
166
167 4. Once you get a login prompt, enter the username catalyst and a
168 password for "catalyst". You should now be at a prompt that looks
169 like:
170
171 catalyst login: catalyst
172 Password: catalyst
173 ...
174 catalyst@catalyst:~$
175
176 5. Type ""ifconfig"" to get the IP address assigned to the virtual
177 machine. You should get output along the lines of:
178
179 eth0 Link encap:Ethernet HWaddr 00:01:22:3b:45:69
180 inet addr:192.168.0.12 Bcast:192.168.0.255 Mask:255.255.255.0
181 ...
182
183 You want the IP address on the second line below the "eth0"
184 interface. The image it design to automatically use a DHCP-
185 assigned address.
186
187 Try to ping this IP address from your "host machine" (main
188 desktop):
189
190 MAINCOMPUTER:~$ ping 192.168.0.12
191 PING 192.168.0.12 (192.168.0.12) 56(84) bytes of data.
192 64 bytes from 192.168.0.12: icmp_req=1 ttl=255 time=4.97 ms
193 64 bytes from 192.168.0.12: icmp_req=2 ttl=255 time=3.43 ms
194 ...
195
196 Note: The ping above is being originated from your host machine
197 (main desktop) and going to your guest virtual machine, not the
198 other way around.
199
200 If you are not seeing a valid IP address or it's not responding to
201 pings (for example, you get error messages along the lines of
202 "Request timed out", "100% packet loss", or "Destination Host
203 Unreachable"), there could be a few network-related issues you
204 might need to sort out. See the section below "Sorting Out Virtual
205 Machine Network-Related Issues" for additional information and
206 troubleshooting advice.
207
208 Note: Remember this IP address... you will be using it throughout
209 the tutorial.
210
211 6. From your main desktop machine, open an SSH client and connect to
212 the IP address found in the previous step. You should get a login
213 prompt (accept the SSH key if you get a warning message about
214 that). Login with the same username and password as we used in
215 Step 4: catalyst / catalyst
216
217 catalyst login: catalyst
218 Password: catalyst
219 ...
220 catalyst@catalyst:~$
221
222 7. Using the SSH session, change to the sample code directory for
223 Chapter 3 included with the Tutorial Virtual Machine and start the
224 Catalyst Development Server:
225
226 $ cd Final/Chapter03/MyApp
227 $ perl script/myapp_server.pl
228
229 8. From your main desktop machine (the "host machine"), open a web
230 browser and go to http://A.B.C.D:3000/, where "A.B.C.D" is the IP
231 address to your virtual machine that you looked up in Step 5. For
232 example, if your virtual machine is using the IP address
233 192.168.0.12, you would put the following URL into your web
234 browser:
235
236 http://192.168.0.12:3000/
237
238 Make sure you don't forget the :3000 to use port 3000 instead of
239 the usual port 80 that is used by HTTP by default.
240
241 You should get a Catalyst Welcome Screen. If you do, feel free to
242 jump right in to Chapter 2 of the tutorial. If you don't go get
243 the Catalyst Welcome Screen, go back and carefully check each of
244 the steps above.
245
246 9. Optional: Also, to reduce download size, the Tutorial VM just
247 includes a minimal command-line environment. You are free to use
248 Debian's very capable "apt" package manager to install other
249 packages. You will first want to pull the apt cache files with
250 "aptitude update" (or "apt-get update" if you prefer apt-get).
251
252 The VI/VIM editor is already installed on the Tutorial Virtual
253 Machine. In order to reduce the size of the download, Emacs is not
254 pre-installed. Since people obviously have very strong opinions
255 about which editor is best, :-) fortunately it's very easy to
256 install Emacs:
257
258 $ sudo aptitude update
259 $ sudo aptitude install emacs
260
261 In general, it is expected that people will boot up the Tutorial VM
262 on their main desktop (the "host machine" using the terminology
263 above) and then use that main desktop machine to SSH and web browse
264 into the "guest VM" as they work through the tutorial. If you wish
265 to install X Windows (or any other packages), just use the
266 "aptitude" (or "apt-get") Debian commands.
267
268 For example, to install X Windows with Fluxbox (a lightweight
269 WindowManager -- it is great for things like this tutorial since
270 it's about 1/10th the size of other common X Windows environments),
271 you can do:
272
273 $ sudo aptitude update
274 $ sudo aptitude install xorg fluxbox iceweasel
275
276 And then start X Windows from the VM Console with this command:
277
278 $ startx
279
280 Note that if you want to start Fluxbox from an SSH session, you can
281 use the "sudo dpkg-reconfigure x11-common" and select "anybody"
282 from the menu. Otherwise, you will need to be on the actual "VM
283 console" to start it.
284
285 If you have a preference for the Gnome desktop environment, you can
286 do:
287
288 $ sudo aptitude update
289 $ sudo aptitude install gnome iceweasel
290 $
291 $ # You can reboot or start with 'startx', we will just reboot here
292 $ reboot
293
294 For KDE, just substitute the package name ""kde"" for ""gnome""
295 above.
296
297 $ sudo aptitude install kde iceweasel
298
299 Note that "iceweasel" is basically used to install Firefox on
300 Debian boxes. You can start it under X Windows with either the
301 "firefox" command or the "iceweasel" command (or use the menus).
302 You can get more information on Iceweasel at
303 <http://wiki.debian.org/Iceweasel>.
304
305 Also, you might need to add more memory to your virtual machine if
306 you want to run X Windows (or other tools that might require
307 additional memory). Consult the documentation of your
308 virtualization software for instructions on how to do this (it's
309 usually pretty simple).
310
311 You may note that the Tutorial Virtual Machine uses local::lib so that
312 the Perl modules are run from ~/perl5 (in this case,
313 /home/catalyst/perl5) vs. the usual location of your "system Perl". We
314 recommend that you also consider using this very handy module. It can
315 greatly ease the process of maintaining and testing different
316 combinations or Perl modules across development, staging, and
317 production servers. (The "relocatable Perl" feature can also be used
318 to run both the modules and Perl itself from your home directory [or
319 any other directory you chose]).
320
321 Note: Please provide feedback on how the Virtual Machine approach for
322 the tutorial works for you. If you have suggestions or comments, you
323 can reach the author through the email address at the bottom of this
324 page or via an RT ticket at
325 <https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual>.
326
327 Sorting Out Virtual Machine Network-Related Issues
328 In general, using a virtual machine to work through the tutorial is
329 *much* easier than trying to do it in other environments, especially if
330 you are new to Catalyst (or Perl or CPAN or ...). However, it's
331 possible that you could run into a few network-related issues. The
332 good news is that there is lots of information about the issue
333 available via search engines on the Internet. Here is some background
334 information to get you started.
335
336 In Step 5 of the prior section above, we assumed that a "Bridged Mode"
337 configuration and DHCP will work (it should for most people). If DHCP
338 is not working or is not available in your location, most virtual
339 machine "host" environments let you select between one of several
340 different types of networking between the "guest" and the "host"
341 machine.
342
343 1) Bridged
344 2) NAT
345 3) Local host only
346
347 The Tutorial Virtual Machine defaults to "Bridged" -- this should
348 result in the VM acting like another device on your network that will
349 get a different DHCP IP address than the host machine. The advantage
350 of this approach, is that you can easily SSH and web browse to the
351 guest virtual machine. In general, this is the best option if you want
352 to be able to boot up the VM and then use your SSH client and web
353 browser from your main machine to connect into the virtual machine.
354
355 In some environments, you might have better luck with "NAT" (Network
356 Address Translation) mode. With this configuration, the guest VM
357 shares the same IP address as the host machine. The downside of this
358 approach is that special configuration is required if you want to be
359 able to SSH or web browse to the guest VM. The NAT option should
360 automatically allow the VM "outbound connection" (e.g., to the Internet
361 if you want to install additional Debian packages), but it requires
362 special configuration if you want to get "inbound connections" that go
363 from some other machine (including the "host machine") into the VM.
364 Some virtual machine host environments let you configure a "static NAT"
365 or "port forwarding" to reach the guest OS, but others omit this
366 functionality.
367
368 Note: NAT mode can work fine if you install X Windows and do the whole
369 tutorial locally on the actual VM vs. using SSH and a web browser from
370 your host machine.
371
372 "Local host only" mode let's the guest VM and the host machine talk on
373 a "private subnet" that other devices in your network cannot reach.
374 This can work as long as you don't need to go from the VM to the
375 Internet (for example, to install other Debian packages).
376
377 Consult the documentation on your virtual machine host environment for
378 help configuring the options above. Here are some links that might
379 help:
380
381 · <http://vmfaq.com/entry/34/>
382
383 · <http://www.vmware.com/support/pubs/player_pubs.html>
384
385 · <http://www.virtualbox.org/manual/ch06.html>
386
388 This tutorial was built using the following resources. Please note that
389 you may need to make adjustments for different environments and
390 versions (note that trailing zeros in version numbers are not
391 significant and may get dropped with some techniques for viewing them;
392 for example, Catalyst v5.80020 might show up as 5.8002):
393
394 · Debian 6 (Squeeze)
395
396 · Catalyst v5.90002
397
398 · Catalyst::Devel v1.34
399
400 · DBIx::Class v0.08195
401
402 · Catalyst::Model::DBIC::Schema v0.54
403
404 · Template Toolkit v2.22
405
406 · HTML::FormFu -- v0.09004
407
408 · NOTE: You can check the versions you have installed with the
409 following command (note the slash before the space):
410
411 perl -M<_mod_name_>\ 999
412
413 or:
414
415 perl -M<_mod_name_> -e 'print "$<_mod_name_>::VERSION\n"'
416
417 For example:
418
419 perl -MCatalyst::Devel\ 999
420
421 or:
422
423 perl -MCatalyst::Devel -e 'print "$Catalyst::Devel::VERSION\n";'
424
425 · This tutorial will show URLs in the format of
426 "http://localhost:3000", but if you are running your web browser
427 from outside the Tutorial Virtual Machine, you will want to
428 substitute the IP address of your VM for the "localhost" in the
429 URLs (again, you can get the IP address for eth0 from the
430 "ifconfig" command). For example, if your VM has an IP address of
431 192.168.0.12, you will want to use a base URL of
432 "http://192.168.0.12:3000". Note that the development server
433 defaults to port 3000 (you can change with the "-p" option on the
434 command line).
435
436 Please Note: Depending on the web browser you are using, you might
437 need to hit "Shift+Reload" or "Ctrl+Reload" to pull a fresh page
438 when testing your application at various points (see
439 <http://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache> for a
440 comprehensive list of options for each browser).
441
442 Also, the "-k" keepalive option to the development server can be
443 necessary with some browsers (especially Internet Explorer).
444
446 This tutorial will primarily focus on SQLite because of its simplicity
447 of installation and use; however, modifications in the script required
448 to support MySQL and PostgreSQL will be presented in the Appendix.
449
450 Note: One of the advantages of using tools like Catalyst and DBIC is
451 that applications become much more database independent. As such, you
452 will notice that only the ".sql" files used to initialize the database
453 change between database systems: most of the code generally remains the
454 same.
455
456 You can jump to the next chapter of the tutorial here: Catalyst Basics
457
459 Kennedy Clark, "hkclark@gmail.com"
460
461 Feel free to contact the author for any errors or suggestions, but the
462 best way to report issues is via the CPAN RT Bug system at
463 <https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual>.
464
465 Copyright 2006-2011, Kennedy Clark, under the Creative Commons
466 Attribution Share-Alike License Version 3.0
467 (<http://creativecommons.org/licenses/by-sa/3.0/us/>).
468
469
470
471perl v5.28.1 2014-1C2a-t1a3lyst::Manual::Tutorial::01_Intro(3)