[Catalyst-dev] Re: Developing action_uri() for Catalyst 5.80

Aristotle Pagaltzis pagaltzis at gmx.de
Thu May 29 10:41:01 GMT 2008


* Ash Berlin <ash_cpan at firemirror.com> [2008-05-29 12:10]:
> Here is my code. It probably only works well for chained
> actions :) Oh and there aren't really any tests for it other
> than my app still works.

That’s a lot of stuff and much of it has a faint whiff of being
in the wrong place, although being that I know the Cat internals
only selectively so far, I can’t be sure.

For reference, here is what I have in MyApp.pm:

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

        if ( not Scalar::Util::blessed( $path ) and 'ARRAY' eq ref $path ) {
            unshift @_, $self->dispatcher->get_action_by_path( shift @$path );
        }

        my $uri = $self->NEXT::uri_for( @_ );

        return $uri;
    }

This exploits the fact that an unblessed arrayref as the first
argument to Catalyst’s `uri_for` is an error to provide the
following syntactic sugar:
 
    # before
    $c->uri_for( $c->dispatcher->get_action_by_path( '/foo/bar'), [ 23 ] )

    # after
    $c->uri_for( [ '/foo/bar', 23 ] )

As is it doesn’t deal with things like relative actions paths and
the like at all, but those things seem to me to properly belong
in a method like `get_action_by_path`, rather than buried inside
a URI construction method where application code cannot use it
for any other purpose. (Eg. `get_action_by_path` could take an
optional second argument that could be a controller instance or
namespace, which would mean “if not absolute, resolve the path
relative to this controller or namespace.”)

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst-dev mailing list