Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

CromeDome (4395)

CromeDome
  (email not shown publicly)
http://www.cromedome.net/
AOL IM: TheOneCromeDome (Add Buddy, Send Message)

Hello, I'm MrCromeDome from Perl Monks. [perlmonks.org] See my account there or my home page for more info.

Journal of CromeDome (4395)

Monday March 09, 2009
07:51 AM

Catalyst Idioms I'm Missing

[ #38620 ]

So I am trying to get a URL that looks like this:

http://localhost:3000/admin/itemtype/32/edit

Instead, I am getting URLs that look like:

http://localhost:3000/admin/itemtype/edit/32

In my template, I tried this:

[% c.uri_for( 'edit', [ item_type.type_id ]) %]

which gave me the latter URL. If I do this:

[% c.uri_for(c.controller('Admin::ItemType').action_for('edit'), [item_type.type_id] ) %]

I get what I want, but it seems too clunky to be doing in my view. Is there a simpler way to write that and have it still work? It seems like too common a thing to not have there be a simpler way, but I am just not finding it.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • You might want to look at chained actions: Catalyst::DispatchType::Chained [cpan.org].

    That gives you an easy way to have URIs like

    /user/$userid/entries/$year/$month

    And you can set the URI via:

    my $action = $ctx->controller('Foo')->action_for('edit');
    my $uri = $ctx->uri_for($action, \@captures, @arguments);

    What goes into captures and what into arguments depends on your application design.

    While the version of uri_for() that uses an action object instead of a string seems a bit more work, it gives you one hug

    --
    Ordinary morality is for ordinary people. -- Aleister Crowley
  • From MyApp.pm:

    sub uri_for {
        my $c = shift;
        my ( $path ) = @_;

        if ( not Scalar::Util::blessed( $path ) and 'ARRAY' eq ref $path ) {
            my $action_path = shift @$path;
            unshift @_, $action_cache->{ $action_path } ||= $c->dispatcher->get_action_by_path( $action_path );
            $path = $_[0];
        }

        return URI::WithBase::Rel->new( $c->NEXT::uri_for( @_ ), $c->req-

    • Woops, that would be:

      sub uri_for {
          my $c = shift;
          my ( $path ) = @_;

          if ( not Scalar::Util::blessed( $path ) and 'ARRAY' eq ref $path ) {
              my $action_path = shift @$path;
              unshift @_, $c->dispatcher->get_action_by_path( $action_path );
              $path = $_[0];
          }

          return URI::WithBase::Rel->new( $c->NEXT::uri_for( @_ ), $c->req->uri );
      }

      (The actual method i