[Catalyst] Catalyst::Action::REST

Robert 'phaylon' Sedlacek rs at 474.at
Mon Nov 20 19:50:18 GMT 2006


Bill Moseley said:

> So what does that look like in real life?  What if process_form() fails
> validation?

Well, this is my plan. Although it's written with :Method syntax, but
that's only a slight difference I assume.

  # ties this stuff to the rest of the controller. also does stuff
  # that auto did in the past.
  sub base: Chained PathPart('foo') CaptureArgs(0) { }

  # form setup
  sub form: Chained('base') PathPart('') CaptureArgs(0) {
    # setup form (e.g. H:W) here
  }

  # handles /foo/create on GET
  # display creation, set defaults in form
  sub create:    Chained('form') Method('GET') { }

  # handles /foo/create on POST
  # try to store. form should contain submitted information. H:W can
  # do this easily for example
  sub do_create: Chained('form') PathPart('create') Method('POST') {
    # if valid, create and redirect
    # otherwise, display form again.
  }

  # loads model into stash. 404 should also be handled here-in, but
  # I'll spare us that.
  sub load: Chained('form') PathPart('') CaptureArgs(1) {
    my ($self, $c, $foo_id) = @_;
    my $foo :Stashed = $c->model('DBIC::Foo')->find($foo_id);
  }

  # handles /foo/123/edit on GET
  sub edit: Chained('load') Method('GET') {
    # fill form with data from my $foo :Stashed
  }

  # handles /foo/123/edit on POST
  sub save: Chained('load') Method('GET') {
    # if valid, store item and redirect
    # otherwise display form with submitted information again
  }

  # handles /foo and returns list of items
  sub list: Chained('base') PathPart('') Method('GET') { }

  # optimize HEAD requests
  sub head: Chained('base') PathPart('') Method('HEAD') { }

To list the chains:
/foo -> base, list|head
/foo/123/edit -> base, form, load, edit|save
/foo/create -> base, form, create|do_create

Might look like many actions at first, but they do very little stuff. I'd
probably also add some controller base magic to do a _NoForm attribute to
tie to load when I don't need a form. A simple 'view' action for example.
Well, ideally the widget system would know how to render as form _and_ as
static display.

gr.,
Robert

-- 
# Robert 'phaylon' Sedlacek
# Perl 5/Catalyst Developer in Hamburg, Germany
{ EMail => ' rs at 474.at ', Web => ' http://474.at ' }




More information about the Catalyst mailing list