X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FCookbook.pod;h=bf6372bc664a3dab5dc107268f4ddc68d6553699;hp=16f37395d1567edc728648c1ca5bd02bea0b4aaa;hb=02bb2b5a140dc22d7d002fcdce868656d704676a;hpb=f4e9de4a3171fd75d04fa8e294fd9a0ae367bc18 diff --git a/lib/Catalyst/Manual/Cookbook.pod b/lib/Catalyst/Manual/Cookbook.pod index 16f3739..bf6372b 100644 --- a/lib/Catalyst/Manual/Cookbook.pod +++ b/lib/Catalyst/Manual/Cookbook.pod @@ -117,56 +117,54 @@ reference. =head3 EXAMPLE - package MyApp; - use Moose; - use namespace::autoclean; - - use Catalyst qw/ - Session - Session::Store::FastMmap - Session::State::Cookie - /; - extends 'Catalyst'; - __PACKAGE__->setup; - - package MyApp::Controller::Foo; - use Moose; - use namespace::autoclean; - BEGIN { extends 'Catalyst::Controller' }; - ## Write data into the session - - sub add_item : Local { - my ( $self, $c ) = @_; - - my $item_id = $c->req->params->{item}; - - push @{ $c->session->{items} }, $item_id; + package MyApp; + use Moose; + use namespace::autoclean; + + use Catalyst qw/ + Session + Session::Store::FastMmap + Session::State::Cookie + /; + extends 'Catalyst'; + __PACKAGE__->setup; + + package MyApp::Controller::Foo; + use Moose; + use namespace::autoclean; + BEGIN { extends 'Catalyst::Controller' }; + ## Write data into the session + + sub add_item : Local { + my ( $self, $c ) = @_; - } + my $item_id = $c->req->params->{item}; - ## A page later we retrieve the data from the session: + push @{ $c->session->{items} }, $item_id; + } - sub get_items : Local { - my ( $self, $c ) = @_; + ## A page later we retrieve the data from the session: - $c->stash->{items_to_display} = $c->session->{items}; + sub get_items : Local { + my ( $self, $c ) = @_; - } + $c->stash->{items_to_display} = $c->session->{items}; + } =head3 More information -L +L -L +L -L +L -L +L -L +L -L +L =head2 Configure your application @@ -176,7 +174,7 @@ separate configuration file. =head3 Using Config::General -L is a method for creating flexible +L is a method for creating flexible and readable configuration files. It's a great way to keep your Catalyst application configuration in one easy-to-understand location. @@ -221,7 +219,7 @@ L explains precedence of multiple sources for configuration values, how to access the values in your components, and many 'base' config variables used internally. -See also L. +See also L. =head1 Skipping your VCS's directories @@ -310,9 +308,9 @@ C<< $c->user >> call. Examples: - Password - Simple username/password checking. - HTTPD - Checks using basic HTTP auth. - TypeKey - Check using the typekey system. + Password - Simple username/password checking. + HTTPD - Checks using basic HTTP auth. + TypeKey - Check using the typekey system. =head3 Storage backends @@ -322,8 +320,8 @@ within this system; you will need to do it yourself. Examples: - DBIC - Storage using a database via DBIx::Class. - Minimal - Storage using a simple hash (for testing). + DBIC - Storage using a database via DBIx::Class. + Minimal - Storage using a simple hash (for testing). =head3 User objects @@ -332,7 +330,7 @@ credential verifier, and is filled with the retrieved user information. Examples: - Hash - A simple hash of keys and values. + Hash - A simple hash of keys and values. =head3 ACL authorization @@ -361,67 +359,67 @@ the user is a member. =head3 EXAMPLE - package MyApp; - use Moose; - use namespace::autoclean; - extends qw/Catalyst/; - use Catalyst qw/ - Authentication - Authorization::Roles - /; + package MyApp; + use Moose; + use namespace::autoclean; + extends qw/Catalyst/; + use Catalyst qw/ + Authentication + Authorization::Roles + /; - __PACKAGE__->config( - authentication => { - default_realm => 'test', - realms => { - test => { - credential => { - class => 'Password', - password_field => 'password', - password_type => 'self_check', - }, - store => { - class => 'Htpasswd', - file => 'htpasswd', - }, - }, - }, - }, - ); + __PACKAGE__->config( + authentication => { + default_realm => 'test', + realms => { + test => { + credential => { + class => 'Password', + password_field => 'password', + password_type => 'self_check', + }, + store => { + class => 'Htpasswd', + file => 'htpasswd', + }, + }, + }, + }, + ); - package MyApp::Controller::Root; - use Moose; - use namespace::autoclean; + package MyApp::Controller::Root; + use Moose; + use namespace::autoclean; - BEGIN { extends 'Catalyst::Controller' } + BEGIN { extends 'Catalyst::Controller' } - __PACKAGE__->config(namespace => ''); + __PACKAGE__->config(namespace => ''); - sub login : Local { - my ($self, $c) = @_; + sub login : Local { + my ($self, $c) = @_; - if ( my $user = $c->req->params->{user} - and my $password = $c->req->param->{password} ) - { - if ( $c->authenticate( username => $user, password => $password ) ) { - $c->res->body( "hello " . $c->user->name ); - } else { - # login incorrect - } - } - else { - # invalid form input - } - } + if ( my $user = $c->req->params->{user} + and my $password = $c->req->param->{password} ) + { + if ( $c->authenticate( username => $user, password => $password ) ) { + $c->res->body( "hello " . $c->user->name ); + } else { + # login incorrect + } + } + else { + # invalid form input + } + } - sub restricted : Local { - my ( $self, $c ) = @_; + sub restricted : Local { + my ( $self, $c ) = @_; - $c->detach("unauthorized") - unless $c->check_user_roles( "admin" ); + $c->detach("unauthorized") + unless $c->check_user_roles( "admin" ); - # do something restricted here - } + # do something restricted here + } =head3 Using authentication in a testing environment @@ -575,7 +573,7 @@ clean up in your C private action instead. Also, it's important to note that if you restrict access to "/" then C, C, etc. will also be restricted. - MyApp->acl_allow_root_internals; + MyApp->acl_allow_root_internals; will create rules that permit access to C, C, and C in the root of your app (but not in any other controller). @@ -821,17 +819,17 @@ This time, the helper sets several options for us in the generated View. =over -=item +=item * INCLUDE_PATH defines the directories that Template Toolkit should search for the template files. -=item +=item * PRE_PROCESS is used to process configuration options which are common to every template file. -=item +=item * WRAPPER is a file which is processed with each template, usually used to easily provide a common header and footer for every page. @@ -856,7 +854,7 @@ or sections; just put in the content. The WRAPPER will the rest of the page around your template for you. -=head3 $c->stash +=head3 C<< $c->stash >> Of course, having the template system include the header and footer for you isn't all that we want our templates to do. We need to be able to @@ -911,7 +909,7 @@ This is the most basic usage, but Template Toolkit is quite powerful, and allows you to truly keep your presentation logic separate from the rest of your application. -=head3 $c->uri_for() +=head3 C<< $c->uri_for() >> One of my favorite things about Catalyst is the ability to move an application around without having to worry that everything is going to @@ -922,7 +920,7 @@ to "/Calendar", "/Calendar/2005", "/Calendar/2005/10", etc. If you move the application to be at http://www.mydomain.com/Tools/Calendar, then all of those links will suddenly break. -That's where $c->uri_for() comes in. This function will merge its +That's where C<< $c->uri_for() >> comes in. This function will merge its parameters with either the base location for the app, or its current namespace. Let's take a look at a couple of examples. @@ -944,7 +942,7 @@ Likewise, The first parameter does NOT have a forward slash, and so it will be relative to the current namespace. If the application is installed at http://www.domain.com/Calendar. and if the template is called from -MyApp::Controller::Display, then the link would become +C, then the link would become http://www.domain.com/Calendar/Display/2005/10/24. If you want to link to a parent uri of your current namespace you can @@ -962,11 +960,11 @@ elements in your site that you want to keep in one file. Further Reading: -L +L -L +L -L +L