3 Catalyst::Manual::Cookbook - Cooking with Catalyst
7 Yummy code like your mum used to bake!
11 =head2 Force debug screen
13 You can force Catalyst to display the debug screen at the end of the request by
14 placing a die() call in the _end action.
18 my ( $self, $c ) = @_;
23 =head2 Disable statistics
25 Just add this line to your application class if you don't want those nifty
26 statistics in your debug messages.
28 sub Catalyst::Log::info { }
32 Scaffolding is very simple with Catalyst.
33 Just use Catalyst::Model::CDBI::CRUD as baseclass.
35 # lib/MyApp/Model/CDBI.pm
36 package MyApp::Model::CDBI;
39 use base 'Catalyst::Model::CDBI::CRUD';
42 dsn => 'dbi:SQLite:/tmp/myapp.db',
51 use Catalyst 'FormValidator';
54 name => 'My Application',
55 root => '/home/joeuser/myapp/root'
60 my ( $self, $c ) = @_;
61 $c->form( optional => [ MyApp::Model::CDBI::Table->columns ] );
62 $c->forward('MyApp::Model::CDBI::Table');
68 Modify the $c->form() parameters to match your needs, and don't forget to copy
71 ==head2 Serving static files and CSS as text/css
73 If you want to serve static content (like images, txt or CSS) via Catalyst,
74 then all you need is the plugin Catalyst::Plugin::Static as well as a small
75 regex to set the MIME type for CSS to text/css.
81 use Catalyst qw/-Debug Static/;
86 my ( $self, $c ) = @_;
90 '/^.*\.css$/' => sub {
91 my ( $self, $c ) = @_;
92 $c->serve_static('text/css');
96 ==head2 Uploads with Catalyst
98 To implement uploads in Catalyst you need to have a HTML form similiar to
101 <form action="/upload" method="post" enctype="multipart/form-data">
102 <input type="hidden" name="form_submit" value="yes">
103 <input type="file" name="my_file">
104 <input type="submit" value="Send">
107 It's very important not to forget enctype="multipart/form-data" in form,
108 if it's not there, uploads just don't work.
110 Catalyst Controller module 'upload' action:
116 if ($c->req->parameters->{form_submit} eq 'yes') {
117 my $filename = $c->req->parameters->{my_file};
119 my $fh = $c->req->uploads->{$filename}->{fh};
120 open(NEW_FILE, ">/tmp/$filename") or die
121 "Can't open file for writing: $!";
122 while ($fh->read(my $buf, 32768)) {
128 $c->stash->{template} = 'upload_form.tt';
129 $c->forward('MyApp::V::View');
133 If you want to upload bigger files than 1MB, then just add to your Controller
136 $CGI::Simple::POST_MAX = 1048576000;
140 Sebastian Riedel, C<sri@oook.de>
144 This program is free software, you can redistribute it and/or modify it under
145 the same terms as Perl itself.