[Catalyst] Catalyst::Request path method as a setter

Bill Moseley moseley at hank.org
Tue Oct 22 15:01:20 GMT 2013


On Mon, Oct 21, 2013 at 9:13 AM, John Napiorkowski <jjn1056 at yahoo.com>wrote:

> My guess here is that path should be RO and that if you need to write code
> that messed with the path, that should happen in plack middleware.
>

Although I do want to let the app know what was removed from the path.

Essentially, (for better or worse) I have existing chained action
/account/123/foo/455 and want to add *optional* path prefixes.   So, that
means accepting /api/v4/account/123/foo/455.  So, the idea is simply pull
off the /api/v4/ from the path and stick it on $req->base base leaving the
original dispatching in place.

The version "v4" is to version specific end points.   Something we don't
want to do very often.   The version would be available in the stash so
that the endpoint can adjust its behavior.

I'm not thrilled by any of that, but that's the solution I'm looking at
right now.




>
> I'll ask around IRC to try and find out why this was allowed in the first
> place.  My guess it that we wanted to allow people to change the path for
> doing complex path rewriting.
>

Thanks.   Just seemed a bit odd that setting $req->path to its current
value would change  $req->uri dramatically.


>    On Monday, October 21, 2013 10:00 AM, Bill Moseley <moseley at hank.org>
> wrote:
>
> =3Dhead2 $req->path
>
>
>
> Returns the path, i.e. the part of the URI after $req->base, for the
> current request.
>
>
> Pasted below is Catalyst::Request's path method.   Note from the final
> else block that $req->path returns the request uri's path ($req->uri->pat=
h)
> with the $req->base->path *removed* as the documentation says.
>
> So, if the request URI and base URI are these:
>
> http://localhost/myapp/path/to/action  # $req->uri
>
> http://localhost/myapp/  # $req->base
>
>
> then $req->path is:
>
> path/to/action
>
>
> Using the example above, and looking at what $req->path( ) does as a
> setter:
>
> $req->path( $req->path );
>
>
> would result in a new request URI of:
>
> http://localhost/path/to/action.
>
>
> The path method doesn't document what it does as a setter, but this
> behavior looks broken because it alters the request URI's path.
>
> What do you think?
>
>
> sub path {
>     my ( $self, @params ) =3D @_;
>
>     if (@params) {
>         $self->uri->path(@params);
>         $self->_clear_path;
>     }
>     elsif ( $self->_has_path ) {
>         return $self->_path;
>     }
>     else {
>         my $path     =3D $self->uri->path;
>         my $location =3D $self->base->path;
>         $path =3D~ s/^(\Q$location\E)?//;
>         $path =3D~ s/^\///;
>         $self->_path($path);
>
>         return $path;
>     }
> }
>
>
>
>
>
> --
> Bill Moseley
> moseley at hank.org
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>


-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20131022/fe09a=
2a6/attachment.htm


More information about the Catalyst mailing list