Re: [Catalyst] stripping path parts and then redispatch?
On 25 Feb 2009, at 01:49, Bill Moseley wrote: At what catalyst version can you use $c->next:: ? Any version where you use MRO::Compat Cheers t0m ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
On Wed, Feb 25, 2009 at 01:12:57AM +, Tomas Doran wrote: > > On 24 Feb 2009, at 18:49, Larry Leszczynski wrote: >> >> I'll be happy to give it a go, if someone can suggest appropriate >> places >> to add this to the POD and/or wiki... > > How about here: http://dev.catalyst.perl.org/wiki/wikicookbook > $c->SUPER::prepare_path(@_); > > Tiny nit pick - you probably want to say $c->next::method(@_) rather > than SUPER::. Or what I sent to Larry: $c->NEXT::prepare_path; At what catalyst version can you use $c->next:: ? -- Bill Moseley mose...@hank.org Sent from my iMutt ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
On 24 Feb 2009, at 18:49, Larry Leszczynski wrote: I'll be happy to give it a go, if someone can suggest appropriate places to add this to the POD and/or wiki... How about here: http://dev.catalyst.perl.org/wiki/wikicookbook $c->SUPER::prepare_path(@_); Tiny nit pick - you probably want to say $c->next::method(@_) rather than SUPER::. I don't think it matters one jot in this case, but this is recommended as general practice :) Cheers t0m ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
On Tue, 24 Feb 2009 05:54 +0200, "Octavian Râsnita" wrote: > I think this solution should be put at least in a wiki if not in the POD > manual. > > Octavian I'll be happy to give it a go, if someone can suggest appropriate places to add this to the POD and/or wiki... Larry > > - Original Message - > From: "Larry Leszczynski" > To: "Bill Moseley" ; "Catalyst Framework" > > Sent: Tuesday, February 24, 2009 3:44 AM > Subject: Re: [Catalyst] stripping path parts and then redispatch? > > > > Just wanted to pass along some solutions... > > > > To recap briefly: > > > > URLs are prefixed with the page language, e.g "/en/foo/bar". The > > language needs to be stripped off and stashed, and the remainder of the > > request processed as if the language part had not been there, e.g. > > "/foo/bar". > > > > I was trying to use $c->go("/foo/bar"), which works if there is a > > Foo::bar() action, but which breaks if the action is supposed to be > > Foo::default() with the argument "bar" (or Foo::Bar::default() with no > > arguments). > > > > > > 1) Bill Moseley suggested a different approach which is seeming to work > > pretty well so far (thanks Bill), it involves subclassing > > MyApp->prepare_path() to tweak the path at beginning of processing, > > something like this: > > > > sub prepare_path > > { > > my $c = shift; > > > > $c->SUPER::prepare_path(@_); > > > > my @path_chunks = split m[/], $c->request->path, -1; > > > > return unless ( @path_chunks > > && $valid_languages{$path_chunks[0]}); > > > > # Pull off first path chunk: > > my $language = shift @path_chunks; > > $c->stash->{language} = $language; > > > > # Create a request path from the remaining chunks: > > my $path = join('/', @path_chunks) || '/'; > > > > # Stuff modified request path back into request: > > $c->request->path($path); > > > > # Update request base to include whatever > > # was stripped from the request path: > > my $base = $c->request->base; > > $base->path($base->path . $language . '/'); > > } > > > > A nice side effect is that tweaking $c->request->base to include the > > language means the language is automatically included when you call > > $c->uri_for(). > > > > > > 2) In the process of trying to get $c->go() to work for an arbitrary > > path, I found a way that seems to make it work consistently even if the > > path is supposed to hit a default action: > > > ># Create your request path to wherever: > >my $path = "/foo/bar/baz"; > > > ># Stuff modified request path back into request: > >$c->request->path($path); > > > ># Create a new $c->action using $c->request->path. > ># Updates $c->action and $c->request->args. > >$c->dispatcher->prepare_action($c); > > > >$c->go($c->action, $c->request->args); > > > > This should work for any of $c->forward(), $c->detach(), $c->visit(), > > $c->go(). > > > > > > Thanks for all the suggestions, > > Larry > > > > ___ > > List: Catalyst@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@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@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/
Re: [Catalyst] stripping path parts and then redispatch?
I think this solution should be put at least in a wiki if not in the POD manual. Octavian - Original Message - From: "Larry Leszczynski" To: "Bill Moseley" ; "Catalyst Framework" Sent: Tuesday, February 24, 2009 3:44 AM Subject: Re: [Catalyst] stripping path parts and then redispatch? Just wanted to pass along some solutions... To recap briefly: URLs are prefixed with the page language, e.g "/en/foo/bar". The language needs to be stripped off and stashed, and the remainder of the request processed as if the language part had not been there, e.g. "/foo/bar". I was trying to use $c->go("/foo/bar"), which works if there is a Foo::bar() action, but which breaks if the action is supposed to be Foo::default() with the argument "bar" (or Foo::Bar::default() with no arguments). 1) Bill Moseley suggested a different approach which is seeming to work pretty well so far (thanks Bill), it involves subclassing MyApp->prepare_path() to tweak the path at beginning of processing, something like this: sub prepare_path { my $c = shift; $c->SUPER::prepare_path(@_); my @path_chunks = split m[/], $c->request->path, -1; return unless ( @path_chunks && $valid_languages{$path_chunks[0]}); # Pull off first path chunk: my $language = shift @path_chunks; $c->stash->{language} = $language; # Create a request path from the remaining chunks: my $path = join('/', @path_chunks) || '/'; # Stuff modified request path back into request: $c->request->path($path); # Update request base to include whatever # was stripped from the request path: my $base = $c->request->base; $base->path($base->path . $language . '/'); } A nice side effect is that tweaking $c->request->base to include the language means the language is automatically included when you call $c->uri_for(). 2) In the process of trying to get $c->go() to work for an arbitrary path, I found a way that seems to make it work consistently even if the path is supposed to hit a default action: # Create your request path to wherever: my $path = "/foo/bar/baz"; # Stuff modified request path back into request: $c->request->path($path); # Create a new $c->action using $c->request->path. # Updates $c->action and $c->request->args. $c->dispatcher->prepare_action($c); $c->go($c->action, $c->request->args); This should work for any of $c->forward(), $c->detach(), $c->visit(), $c->go(). Thanks for all the suggestions, Larry ___ List: Catalyst@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@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/
Re: [Catalyst] stripping path parts and then redispatch?
Just wanted to pass along some solutions... To recap briefly: URLs are prefixed with the page language, e.g "/en/foo/bar". The language needs to be stripped off and stashed, and the remainder of the request processed as if the language part had not been there, e.g. "/foo/bar". I was trying to use $c->go("/foo/bar"), which works if there is a Foo::bar() action, but which breaks if the action is supposed to be Foo::default() with the argument "bar" (or Foo::Bar::default() with no arguments). 1) Bill Moseley suggested a different approach which is seeming to work pretty well so far (thanks Bill), it involves subclassing MyApp->prepare_path() to tweak the path at beginning of processing, something like this: sub prepare_path { my $c = shift; $c->SUPER::prepare_path(@_); my @path_chunks = split m[/], $c->request->path, -1; return unless ( @path_chunks && $valid_languages{$path_chunks[0]}); # Pull off first path chunk: my $language = shift @path_chunks; $c->stash->{language} = $language; # Create a request path from the remaining chunks: my $path = join('/', @path_chunks) || '/'; # Stuff modified request path back into request: $c->request->path($path); # Update request base to include whatever # was stripped from the request path: my $base = $c->request->base; $base->path($base->path . $language . '/'); } A nice side effect is that tweaking $c->request->base to include the language means the language is automatically included when you call $c->uri_for(). 2) In the process of trying to get $c->go() to work for an arbitrary path, I found a way that seems to make it work consistently even if the path is supposed to hit a default action: # Create your request path to wherever: my $path = "/foo/bar/baz"; # Stuff modified request path back into request: $c->request->path($path); # Create a new $c->action using $c->request->path. # Updates $c->action and $c->request->args. $c->dispatcher->prepare_action($c); $c->go($c->action, $c->request->args); This should work for any of $c->forward(), $c->detach(), $c->visit(), $c->go(). Thanks for all the suggestions, Larry ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
Hi Jason - > >>> I have an existing site, and want to add the page language to the URLs > >>> so that caching will work correctly, e.g. "/foo/bar" would now look like > >>> "/en/foo/bar" or "/fr/foo/bar". > >> Apart from the missing true return value from auto, this works: > >> > >> http://lists.scsys.co.uk/pipermail/catalyst/2009-February/021072.html > > > > Thanks for the pointer. But this approach only seems to work for Local > > actions, not Private actions like default(). E.g. using these > > controllers: > > > > # > > > > package MyApp::Controller::Foo; > > use parent 'Catalyst::Controller'; > > > > sub default :Private { > > my ($self, $c, @args) = @_; > > $c->response->body("Foo args: @args"); > > } > > That construct is deprecated. You can do basically the same thing with: > >sub default : Path { >... >} > > See: > http://search.cpan.org/dist/Catalyst-Manual/lib/Catalyst/Manual/Intro.pod#Built-in_Private_Actions Thanks, acknowledged. However, changing "default:Private" to "default:Path" doesn't solve the problem - the expected argument to $c->go() for the path "/foo" is still "/foo/default", which is not obvious just from looking at the path. Larry ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
Larry Leszczynski wrote: Hi Lars - I have an existing site, and want to add the page language to the URLs so that caching will work correctly, e.g. "/foo/bar" would now look like "/en/foo/bar" or "/fr/foo/bar". Apart from the missing true return value from auto, this works: http://lists.scsys.co.uk/pipermail/catalyst/2009-February/021072.html Thanks for the pointer. But this approach only seems to work for Local actions, not Private actions like default(). E.g. using these controllers: # package MyApp::Controller::Foo; use parent 'Catalyst::Controller'; sub default :Private { my ($self, $c, @args) = @_; $c->response->body("Foo args: @args"); } That construct is deprecated. You can do basically the same thing with: sub default : Path { ... } See: http://search.cpan.org/dist/Catalyst-Manual/lib/Catalyst/Manual/Intro.pod#Built-in_Private_Actions -- Jason Gottshall jgottsh...@capwiz.com ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
Hi Lars - > > I have an existing site, and want to add the page language to the URLs > > so that caching will work correctly, e.g. "/foo/bar" would now look like > > "/en/foo/bar" or "/fr/foo/bar". > > Apart from the missing true return value from auto, this works: > > http://lists.scsys.co.uk/pipermail/catalyst/2009-February/021072.html Thanks for the pointer. But this approach only seems to work for Local actions, not Private actions like default(). E.g. using these controllers: # package MyApp::Controller::Foo; use parent 'Catalyst::Controller'; sub default :Private { my ($self, $c, @args) = @_; $c->response->body("Foo args: @args"); } sub bar :Local { my ($self, $c, @args) = @_; $c->response->body("Foo::Bar args: @args"); } # package MyApp::Controller::En; use parent 'Catalyst::Controller'; sub default :Private { my ($self, $c, @args) = @_; my $path = $c->request->path; $path =~ s{^en}{}; $c->go($path, []); } # The paths "/en/foo/bar" or "/en/foo/bar/arg1" work as expected, but if I try to hit "/en/foo" I get an error message: Couldn't go to command "/foo": Invalid action or component. because the action name it wants is really "/foo/default", not just "/foo". For a path like "/a/b/c", I would need to figure out whether to do this: # Will hit: A::Bwith arg 'c' # or: A::B::C with no args # $c->go("/a/b/c", []); vs this: $c->go("/a/default", ['b', 'c']); vs this: $c->go("/a/b/default", ['c']); ? I'm guessing there must be a way to use the Dispatcher to figure out, for a given path, how to make go() work consistently, but I have not yet found the trick for making this work... Thanks! Larry ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
On Sun, Feb 22, 2009 at 11:28 PM, Larry Leszczynski wrote: > I have an existing site, and want to add the page language to the URLs > so that caching will work correctly, e.g. "/foo/bar" would now look like > "/en/foo/bar" or "/fr/foo/bar". (The language is user-selected, not > from browser prefs, so setting the "Vary: Accept-Language" response > header won't help.) Apart from the missing true return value from auto, this works: http://lists.scsys.co.uk/pipermail/catalyst/2009-February/021072.html -- Lars Balker RasmussenConsult::Perl ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
On Sun, Feb 22, 2009 at 03:28:48PM -0700, Larry Leszczynski wrote: > I'd like to be able to capture and strip off the language, in e.g. > MyApp::Controller::Root::auto(), and then either forward or redispatch > to the existing controllers, but haven't been able to come up with a way > of generating the forwarding path/args that consistently works for all > action types. E.g., how do I know if I should dispatch to action > "/foo/bar", or action "/foo" with argument "bar"? I'm sure there's better approach, but I've simply overridden prepare_path and then removed the prefix (e.g. the language) from the path, set a flag, and then appended it on $req->base. -- Bill Moseley mose...@hank.org Sent from my iMutt ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
Hi Tomas - > > I'm using Catalyst 5.8.5 so I can make use of any of forward, detach, > > visit, go, etc. > > Not helpful to your main email, but there is no such version as 5.8.5? > > I assume you mean 5.8000_05, which is a developer release? Yes, that's what I meant, sorry. Larry ___ List: Catalyst@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/
Re: [Catalyst] stripping path parts and then redispatch?
On 22 Feb 2009, at 22:28, Larry Leszczynski wrote: I'm using Catalyst 5.8.5 so I can make use of any of forward, detach, visit, go, etc. Not helpful to your main email, but there is no such version as 5.8.5? I assume you mean 5.8000_05, which is a developer release? Cheers t0m ___ List: Catalyst@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/