Request

View: New views
7 Messages — Rating Filter:   Alert me  

Request

by Octavian Râşniţă :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Is there a Catalyst Request method for getting the path and the
query_string?
(Everything's after the base).

I want to use it in a TT template for changing the current language.

The current URI is something like:
http://www.site.com/prg?var1=val1&var2=val2

The base for this URI is:
http://www.site.com/en/
(Because I overwritten prepare_path as in the example given on the Cat wiki)

and the URL that should be printed with the new language is:
http://www.site.com/ro/prg?var1=val1&var2=val2

So I've got the base in the template, replaced '/../$' with 'ro' and then I
need to add the path but also the query_string.

Thanks.

Octavian


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

Re: Request

by Evan Carroll-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Is there a Catalyst Request method for getting the path and the
> query_string?
> (Everything's after the base).

$c->req->uri->path_query

--
Evan Carroll
System Lord of the Internets
http://www.evancarroll.com

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

Re: Request

by Larry Leszczynski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Octavian -

> Is there a Catalyst Request method for getting the path and the
> query_string? (Everything's after the base).
>
> I want to use it in a TT template for changing the current language.
>
> The current URI is something like:
> http://www.site.com/prg?var1=val1&var2=val2
>
> The base for this URI is:
> http://www.site.com/en/
> (Because I overwritten prepare_path as in the example given on the Cat
> wiki)
>
> and the URL that should be printed with the new language is:
> http://www.site.com/ro/prg?var1=val1&var2=val2

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.


HTH,
Larry

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

Re: Request

by Octavian Râşniţă :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

From: "Larry Leszczynski" <larryl@...>

> Hi Octavian -
>
>> Is there a Catalyst Request method for getting the path and the
>> query_string? (Everything's after the base).
>>
>> I want to use it in a TT template for changing the current language.
>>
>> The current URI is something like:
>> http://www.site.com/prg?var1=val1&var2=val2
>>
>> The base for this URI is:
>> http://www.site.com/en/
>> (Because I overwritten prepare_path as in the example given on the Cat
>> wiki)
>>
>> and the URL that should be printed with the new language is:
>> http://www.site.com/ro/prg?var1=val1&var2=val2
>
> 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.

Thank you Evan. Your solution was helpful.
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.

Octavian


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

Re: Request

by Larry Leszczynski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Octavian -

On Tue, 27 Oct 2009 23:16 +0200, "Octavian Râsnita" <orasnita@...>
wrote:

> From: "Larry Leszczynski" <larryl@...>
> > Hi Octavian -
> >
> >> Is there a Catalyst Request method for getting the path and the
> >> query_string? (Everything's after the base).
> >>
> >> I want to use it in a TT template for changing the current language.
> >>
> >> The current URI is something like:
> >> http://www.site.com/prg?var1=val1&var2=val2
> >>
> >> The base for this URI is:
> >> http://www.site.com/en/
> >> (Because I overwritten prepare_path as in the example given on the Cat
> >> wiki)
> >>
> >> and the URL that should be printed with the new language is:
> >> http://www.site.com/ro/prg?var1=val1&var2=val2
> >
> > 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 Evan. Your solution was helpful.
> 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

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

Re: Request

by Octavian Râşniţă :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

From: "Larry Leszczynski" <larryl@...>
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


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

Re: Request

by Tomas Doran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 27 Oct 2009, at 21:16, Octavian Râsnita wrote:
>
> 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.

http://search.cpan.org/~flora/Catalyst-Runtime-5.80013/lib/Catalyst/ 
Request.pm#$req-%3Euri

 > $req->uri
 >
 > Returns a URI object for the current request. Stringifies to the  
URI text.
I've just committed a change (r11681) to make this doc link to the  
docs for URI.pm to make it even more obvious.

Cheers

t0m



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