X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FIntro.pod;h=182b14a94fab2352c304e928967500a6c6154b57;hp=36adc7ce83b287d198894184eac1715e56b610c2;hb=709ea2fce465890b806df6320bb96b0f1a7d27ca;hpb=b411df01b40662f125aa854a7c25097bc53ad86a diff --git a/lib/Catalyst/Manual/Intro.pod b/lib/Catalyst/Manual/Intro.pod index 36adc7c..182b14a 100644 --- a/lib/Catalyst/Manual/Intro.pod +++ b/lib/Catalyst/Manual/Intro.pod @@ -97,18 +97,18 @@ doesn't require mod_rewrite or class and method names in URLs. With Catalyst you register your actions and address them directly. For example: - sub hello : Global { + sub hello : Local { my ( $self, $context ) = @_; $context->response->body('Hello World!'); } Now http://localhost:3000/hello prints "Hello World!". -Note that actions with the C< :Local > attribute are equivalent to -using a C<:Path('/action_name') > attribute (note the leading slash). -So our action could be equivalently: +Note that actions with the C< :Global > attribute are equivalent to +using a C<:Path('action_name') > attribute, so our action could be +equivalently: - sub hello : Path('/hello') { + sub hi : Path('hello') { my ( $self, $context ) = @_; $context->response->body('Hello World!'); } @@ -116,8 +116,11 @@ So our action could be equivalently: =item * B -Use L or L. Other -engines are also available. +Use L or L. Another +interesting engine is L - available from CPAN +separately - which will turn the built server into a fully fledged production +ready server (although you'll probably want to run it behind a front end proxy +if you end up using it). =back @@ -526,27 +529,54 @@ instance of the model. If the component supports the C method instead of returning the model itself, the return value of C<< $model->ACCEPT_CONTEXT( $c ) >> will be used. -This means that whenever your model/view/controller needs to talk to C<$c> it -gets a chance to do this when it's needed. +This means that whenever your model/view/controller needs to talk to +C<$c> it gets a chance to do this when it's needed. A typical C method will either clone the model and return one with the context object set, or it will return a thin wrapper that contains C<$c> and delegates to the per-application model object. -A typical C method could look like this: - - sub ACCEPT_CONTEXT { - my ( $self, $c, @extra_arguments ) = @_; - bless { %$self, c => $c }, ref($self); - } - -effectively treating $self as a B that gets a new parameter. -C<@extra_arguments> comes from any trailing arguments to -C<< $c->component( $bah, @extra_arguments ) >> (or C<< $c->model(...) >>, -C<< $c->view(...) >> etc). - -The life time of this value is B, and not per request. To make this -per request you can use the following technique: +Generally it's a bad idea to expose the context object (C<$c>) in your +model or view code. Instead you use the C subroutine +to grab the bits of the context object that you need, and provide +accessors to them in the model. This ensures that C<$c> is only in +scope where it is neaded which reduces maintenance and debugging +headaches. So, if for example you needed two +L models in the same Catalyst model +code, you might do something like this: + + __PACKAGE__->mk_accessors(qw(model1_schema model2_schema)); + sub ACCEPT_CONTEXT { + my ( $self, $c, @extra_arguments ) = @_; + $self = bless({ %$self, + model1_schema => $c->model('Model1')->schema, + model2_schema => $c->model('Model2')->schema + }, ref($self)); + return $self; + } + +This effectively treats $self as a B that gets a new +parameter. C<@extra_arguments> comes from any trailing arguments to +C<< $c->component( $bah, @extra_arguments ) >> (or C<< $c->model(...) +>>, C<< $c->view(...) >> etc). + +In a subroutine in the model code, we can then do this: + + sub whatever { + my ($self) = @_; + my $schema1 = $self->model1_schema; + my $schema2 = $self->model2_schema; + ... + } + +Note that we still want the Catalyst models to be a thin wrapper +around classes that will work independently of the Catalyst +application to promote reusability of code. Here we might just want +to grab the $c->model('DB')->schema so as to get the connection +information from the Catalyst application's configuration for example. + +The life time of this value is B, and not per request. To +make this per request you can use the following technique: Add a field to C<$c>, like C. Then write your C method to look like this: @@ -564,6 +594,9 @@ C method to look like this: } } +For a similar technique to grab a new component instance on each +request, see L. + =head3 Application Class In addition to the Model, View, and Controller components, there's a @@ -652,7 +685,7 @@ information. $c->res->status(404); $c->res->redirect('http://oook.de'); -=item * L +=item * config $c->config $c->config->{root}; @@ -927,7 +960,7 @@ Called at the end of a request, after all matching actions are called. =head4 Built-in actions in controllers/autochaining - Package MyApp::Controller::Foo; + package MyApp::Controller::Foo; sub begin : Private { } sub default : Path { } sub auto : Private { } @@ -962,15 +995,13 @@ would be called: =item for a request for C - MyApp::begin - MyApp::auto + MyApp::Controller::Foo::auto MyApp::Controller::Foo::default # in the absence of MyApp::Controller::Foo::Foo - MyApp::end + MyApp::Controller::Foo::end =item for a request for C MyApp::Controller::Foo::Bar::begin - MyApp::auto MyApp::Controller::Foo::auto MyApp::Controller::Foo::Bar::auto MyApp::Controller::Foo::Bar::default # for MyApp::Controller::Foo::Bar::foo @@ -990,7 +1021,6 @@ like this: false MyApp::Controller::Foo::Bar::begin - MyApp::auto MyApp::Controller::Foo::Bar::end =back @@ -1194,6 +1224,14 @@ Mailing lists: http://lists.scsys.co.uk/mailman/listinfo/catalyst http://lists.scsys.co.uk/mailman/listinfo/catalyst-dev +Wiki: + + http://dev.catalystframework.org/wiki + +FAQ: + + http://dev.catalystframework.org/wiki/faq + =head1 AUTHOR Sebastian Riedel, C