From: Danijel Milicevic Date: Thu, 3 Mar 2005 09:52:02 +0000 (+0000) Subject: New entries for the cookbook X-Git-Tag: 5.7099_04~1788 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=aba949649162391bd2e93783e98aa12e33abbd17 New entries for the cookbook --- diff --git a/lib/Catalyst/Manual/Cookbook.pod b/lib/Catalyst/Manual/Cookbook.pod index 28f43e3..9f8b0eb 100644 --- a/lib/Catalyst/Manual/Cookbook.pod +++ b/lib/Catalyst/Manual/Cookbook.pod @@ -4,7 +4,7 @@ Catalyst::Manual::Cookbook - Cooking with Catalyst =head1 DESCRIPTION -Yummy! +Yummy code like your mum used to bake! =head1 RECIPES @@ -68,6 +68,73 @@ Just use Catalyst::Model::CDBI::CRUD as baseclass. Modify the $c->form() parameters to match your needs, and don't forget to copy the templates. ;) +==head2 Serving static files and CSS as text/css + +If you want to serve static content (like images, txt or CSS) via Catalyst, +then all you need is the plugin Catalyst::Plugin::Static as well as a small +regex to set the MIME type for CSS to text/css. + + # lib/MyApp.pm + package MyApp; + + use strict; + use Catalyst qw/-Debug Static/; + + __PACKAGE__->action( + + '!default' => sub { + my ( $self, $c ) = @_; + $c->serve_static; + }, + + '/^.*\.css$/' => sub { + my ( $self, $c ) = @_; + $c->serve_static('text/css'); + }, + ); + +==head2 Uploads with Catalyst + +To implement uploads in Catalyst you need to have a HTML form similiar to +this: + +
+ + + +
+ +It's very important not to forget enctype="multipart/form-data" in form, +if it's not there, uploads just don't work. + +Catalyst Controller module 'upload' action: + + MyApp->action( + + 'upload' => sub { + my ($self, $c) = @_; + if ($c->req->parameters->{form_submit} eq 'yes') { + my $filename = $c->req->parameters->{my_file}; + if ($filename) { + my $fh = $c->req->uploads->{$filename}->{fh}; + open(NEW_FILE, ">/tmp/$filename") or die + "Can't open file for writing: $!"; + while ($fh->read(my $buf, 32768)) { + print NEW_FILE $buf; + } + close(NEW_FILE); + } + } + $c->stash->{template} = 'upload_form.tt'; + $c->forward('MyApp::V::View'); + }, + ); + +If you want to upload bigger files than 1MB, then just add to your Controller +module: + + $CGI::Simple::POST_MAX = 1048576000; + =head1 AUTHOR Sebastian Riedel, C