List Info

Thread: uri_for() and Chained endpoints: a proposed convenience method




uri_for() and Chained endpoints: a proposed convenience method
user name
2007-01-26 14:41:11
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!

Thanks,
Jason

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: uri_for() and Chained endpoints: a proposed convenience method
user name
2007-01-26 14:51:46
On 26 Jan 2007, at 20:41, Jason Gottshall wrote:

> In using chained dispatch, I found that getting URIs
for specific
> endpoints felt a bit clumsy:
>
> 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
>     );
>   }

I think the last long discussion on the -dev list about this
 
(archives are public and googlable  resulted
in us calling it  
uri_to and not trying to make it Chained-specific, and
wanting to  
make it more intelligent than yours.

Maybe you could subscribe to -dev and once we finally thrash
out what  
we want to do with this you could help with
implementation/test-cases/ 
whatever?

But yeah, it -is- a sensible idea and we already want
something in  
the dist and we'd love you to help out with getting it in
there 

-- 
Matt S Trout, Technical Director, Shadowcat Systems Ltd.
Offering custom development, consultancy and support
contracts for  
Catalyst,
DBIx::Class and BAST. Contact mst (at)
shadowcatsystems.co.uk for  
details.
+ Help us build a better perl ORM: http://dbix- 
class.shadowcatsystems.co.uk/ +



_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: uri_for() and Chained endpoints: a proposed convenience method
user name
2007-01-26 14:56:54
On 1/26/07, Jason Gottshall <jgottshallcapwiz.com> wrote:
> 1) Is my convenience method a sensible approach, or is
there a "better"
> way to do this?

I usually stuff this inside MyApp.pm:

  sub uri_for_action {
      my ($c, $controller, $action, params) = _;
      $c->uri_for(
$c->controller($controller)->action_for($action), params
);
  }

It works pretty nicely for me. Its only drawback is that if
you're
doing some refactoring and decide to move some methods to a
new
controller, you'd probably need to change whenever it was
called. But
it's grepable and pretty reasonable IMO.

-Nilson Santos F. Jr.

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

Re: uri_for() and Chained endpoints: a proposed convenience method
user name
2007-01-26 14:59:30
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/br
owser/branches/Catalyst-Runtime-ctx-uri_for_action

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

Ash

_______________________________________________
List: Catalystlists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalystlists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )