Jason Gottshall wrote:
In using chained dispatch, I found that getting URIs for specific
endpoints felt a bit clumsy:

  package MyApp::C::Foo;

  sub get_foo : Chained('/')   PathPart('foo') CaptureArgs(0) {}
  sub do_bar  : Chained('foo') PathPart('bar') Args(2) {}

  package MyApp::C::Qux

  sub index: Private {
    my ( $self, $c ) = @_;
    my $query = { search => 'something' };

    # redirect to "/foo/bar/baz/qux?search=something"
$c->res->redirect( $c->uri_for->( $c->controller('Foo')->action_for('do_bar'), [qw/baz qux/],
$query
) );
  }

I wanted a way to condense the fairly wordy call to
$c->controller()->action_for(), so I added the following convenience
method to MyApp.pm:

  sub uri_for_endpoint {
    my ( $c, $endpoint, @args ) = @_;
    my ( $controller, $action ) = ( split m[/], $endpoint, 2 )[ -2, -1
];
return $c->uri_for( $c->controller($controller)->action_for($action), @args
    );
  }

Now I can rewrite the call to uri_for() like this:

# $c->uri_for->( # $c->controller('Foo')->action_for('do_bar'), [qw/baz qux/],
$query
  #   )
      $c->uri_for_endpoint( 'foo/do_bar', [qw/baz qux/], $query )

My questions to the list are as follows:

1) Is my convenience method a sensible approach, or is there a "better"
way to do this?
2) The method I wrote seems to work correctly. Did I miss anything?
3) Is this useful enough to other folks to be worth adding to the dist
somewhere?

All feedback (including things like "You're too lazy") appreciated!
I made an attempt at this before - my code is in http://dev.catalyst.perl.org/browser/branches/Catalyst-Runtime-ctx-uri_for_action

See inparticular http://dev.catalyst.perl.org/browser/branches/Catalyst-Runtime-ctx-uri_for_action/t/unit_core_uri_for_action.t (I think)

Ash

_______________________________________________
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/

Reply via email to