[Catalyst] Request
Octavian Râsnita
orasnita at gmail.com
Wed Oct 28 08:49:24 GMT 2009
From: "Larry Leszczynski" <larryl at emailplus.org>
Hi Octavian -
> > Not sure if this gets you all the way there, but you could call
> > "c.req.uri" with no arguments, that should give you the current request
> > including base, path and query string. Then you could strip off
> > c.req.base from the beginning of that string.
>
> Thank you Larry, but the problem is that the base was overwritten and it
> contains some more than the uri, so I can't cut it from the URI.
It should work fine, we do something very similar. The trick is that
after the prepare_path fixup (if you did it like in the wiki), when you
stringify c.req.uri, it will reflect the *new* base, not the original.
Using your example:
original request url: http://www.site.com/prg?var1=val1&var2=val2
rewritten request url:
http://www.site.com/en/prg?var1=val1&var2=val2
So you should have:
[%
uri = c.req.uri; #
http://www.site.com/en/prg?var1=val1&var2=val2
base = c.req.base; # http://www.site.com/en/
pattern = '^' _ base; # pattern not
tested...
path_and_query = uri.replace(pattern, '');
%]
So it's easy to strip "base" from the front of "uri" and get what you
need, without knowing what "base" is.
**
Thank you Larry. Finally I've done it this way.
> I didn't know that c.req.uri is not just a string, but an object that has
> its own methods, and c.req.uri.path_query was the one I needed.
This could work, but given the same original url and prepare_path fixup,
you will have:
[%
uri = c.req.uri; #
http://www.site.com/en/prg?var1=val1&var2=val2
path_and_query = c.req.uri.path_query; #
/en/prg?var1=val1&var2=val2
%]
So now when you build the new url you have to be aware that
path_and_query contains the "/en" part that needs to be stripped.
Larry
**
Oh yes, I found that so I've used the solution you proposed.
Now I use the following line in prepare_path() for every request:
$c->request->uri->path("$language/" . $c->request->path);
so the URI is always overwritten and it contains the language indicator.
I had previously used that line like
unless(@path_chunks && $valid_languages{$path_chunks[0]}) {
$c->request->uri->path("$language/" . $c->request->path);
}
and in that case the URI wasn't containing the language indicator when the
original URI was without a language indicator, but the base was always
containing it.
But now it seems to work fine.
Thank you.
Octavian
More information about the Catalyst
mailing list