[Catalyst] Componentised actions.

Robert 'phaylon' Sedlacek rs at 474.at
Thu Mar 29 15:41:00 GMT 2007


Oleg Pronin wrote:

> I'm developing a website based on catalyst and want it to be moduled and
> componentised the way like this:
>  
> site.com/object1/list <http://site.com/object1/list> 
>    displays objects 1 in empty design.
>  
> site.com/object2/list <http://site.com/object2/list> 
>    displays objects 2 in empty design.

Are 'object1' and 'object2' identifiers? Are those objects of the same
class? If so, try Catalyst::DispatchType::Chained; Example (untested):

  package MyApp::Controller::Foo;
  use strict;
  use base 'Catalyst::Controller';

  sub load_object : Chained PathPart('object') CaptureArgs(1) {
      my ($self, $c, $object_id) = @_;
      $c->stash( object => load_object_from_somewhere($object_id);
  }

  sub list : Chained('load_object') Args(0) {
      my ($self, $c) = @_;
      # do something with object
  }

  1;

If those two objects are from a different kind, but share common 'list'
logic, you can abstract that out into a controller base class and
inherit the list action in two controllers that provide a load_object
action.

> site.com/nicepage <http://site.com/nicepage>
>    displays a page in website design which contains result of processing
> actions 'site.com/object1/list <http://site.com/object1/list>' and
> 'site.com/object2/list <http://site.com/object2/list>'.
>  
> The question one is how do i get the output of the actions inserted into
> another template ?
>  
> i mean something like
>  
> page template page template
> [% c.forward('/object1/list') %]
> page template page template,
>  
> just for example, this of course doesn't work.

Can't you just forward from your nicepage action to the list actions and
let them prepare resources in the stash? With that, you could just
include their templates in your template.

> The question two is how do i know while running in '/object2/list' action
> whether action is called from the browser or inserted into another template.

IMO you shouldn't need to know. Communicate with the rest of the app via
the stash. If a template only uses information in the stash (and maybe
config), it doesn't need to know whether it is the rendered template or
just an included one.

> I.e. i want URL site.com/object1/list <http://site.com/object1/list> to
> display information in site design while the same action inserted into
> another template
> ( pseudo :) [% c.forward('/object1/list') %] )  to display information
> in empty desing (i.e. through another VIEW).

What is an 'empty design'? And you probably don't need another View
either. As said above, I think you should be fine just by separating
your logic a bit more.

.phaylon



More information about the Catalyst mailing list