[Catalyst] Form handling, urls and web design

Alex Kavanagh alex at tinwood.homelinux.org
Tue Jan 24 10:04:37 CET 2006


At Mon, 23 Jan 2006 15:47:40 -0800,
Bill Moseley wrote:
> 
> On Mon, Jan 23, 2006 at 09:47:17PM +0000, Alex Kavanagh wrote:
> > At the moment, I have two URLs to deal with this:
> > 
> > /manage/users             - provide the list of users
> > /manage/users/<username>  - deal with a particular user.
> > 
> > I have a controller called Manage::Users.pm which appropriately looks
> > after the /manage/users url fragment.
> > 
> > Currently, I have two main functions which are called by the Catalyst
> > framework:
> > 
> > sub default : Private {
> > }
> > 
> > looks after the list of users
> > 
> > and
> > 
> > sub user : LocalRegex( '(\w+)$' ) {  # ';
> > }
> > 
> > to handle the user.
> 
> I stay away from default.  Use an empty path instead.

Why do you stay away from default?

> 
> How about:
> 
>     # Display a list of users for /manage/users
>     sub list : Path {
>         my ( $self, $c ) = @;
> 
> 
>     # Edit, Create, Update a user
>     sub edit : Local {
>         my ( $self, $c, $user ) = @_;
> 
> 
> In edit() if $user is passed in then fetch the user and display the
> form populated.  If no user is passed in show a blank form for
> create.  FillInForm works wonders here.

Yes, I tried to do something similar and then use my magic selector
(which is basically just an if statement) to select the right action
to call.

> 
> 
> I use the generic "list" and "edit" names.  Then templates end up
> consistent: 
> 
>     $template_root/manage/users/list.tt
>     $template_root/manage/users/edit.tt
> 
>     $template_root/manage/stuff/list.tt
>     $template_root/manage/stuff/edit.tt
> 
> 
> I use the same "edit" action for edit/create/update.  So my edit
> actions look like:
> 
>     sub edit : Local {
>         my ( $self, $c, $id ) = @_;
> 
>         my $form = $c->stash->{form} = App::Form::Admin::Organization->new( $id );
> 
>         return $c->internal_redirect('list', { message => 'Invalid Organization Selected' } )
>             unless $form;
> 
>         # Now validate and update
>         $form->update_from_form( $c->req->parameters )
>             if $c->form_posted;
>     }
> 

Um, where is internal_redirect defined?  Is it a plugin? What makes it
different from a normal $c->res->redirect ?

> 
> That shows the form again after update.  Otherwise, I'd do a
> redirect if update_from_form returns true.  If it returns false then
> the form didn't validate and it needs to be redrawn.

This certainly makes sense.  I'll have to ponder how to change what
I'm doing to simplify things.

Many thanks
Cheers
Alex.



More information about the Catalyst mailing list