[Catalyst] How to pass args/data from one controller to another?

Dustin Suchter dsuchter at ucla.edu
Sun Sep 30 04:16:01 GMT 2007


Ummm... that's what I logically thought was the "right" solution,
but I tried that already - it doesn't work!! Um... I also notice a
spelling mistake for "Libaray" in my example, but that's me just
messing up when renaming from my real code to the examples I'm using
in email.

#####
#in MyApp::Controller::Libaray
my $url_working = $c->uri_for('/readabook');
my $url_broken1 = $c->uri_for('/readabook', "$book_id", "$page_id");
my $url_broken2 = $c->uri_for('/readabook', '$book_id', '$page_id');
$c->log->debug("urlworking: '$url_working', urlbroken1:
'$url_broken1', urlbroken2: '$url_broken2'");
#####

#####
#debug output
[debug] urlworking: 'http://server.com:3000/readabook', urlbroken1:
'http://server.com:3000/readabook/1/1', urlbroken2:
'http://server.com:3000/readabook/$book_id/$page_id'
#####

As you can see from the variable names, only the first version
(which of course doesn't pass any of the necessary args), actually
works. Although "$url_broken1" seems to generate the right URI, the
actual doesn't get "caught" by the ReadABook controller in the
correct way, and Catalyst ends up serving the default URI (home),
which is a method in the Library controller.

-d

Antano Solar wrote:
>> That's what I was trying before, but check this out:
>>
>> # CODE 1
>> #in MyApp::Controller::Libaray
>> sub read_book : Local {
>>     my ($self, $c) = @_;
>>     $c->response->redirect($c->uri_for('/readabook/read_book' ));
>> }
>>
>> # CODE 2
>> #in MyApp::Controller::Libaray
>> sub read_book : Local {
>>     my ($self, $c) = @_;
>>     my ($bid, $pid) = ("2","345");
>> $c->response->redirect($c->uri_for("/readabook/read_book/$bid/$pid"));
>> }
>>
>> # CODE 3
>> #in MyApp::Controller::ReadABook
>> sub read_book : Local {
>>     my ($self, $c, $book_id, $page_id) = @_;
>>     #do stuff...
>> }
>>
>> When I use CODE 1 the app correctly executes CODE 3, but of course
>> there are no args passed along, so $book_id and $page_id are
>> uninitalized. When I use CODE 2, Catalyst executes my default action
>> (home) - my guess it it does this because it can't find a specific
>> "/readabook/read_book/2/345". For the record I also tried using CODE
>> 1 and stuffing the values I needed into the stash, but that didn't
>> work either.
>>
>> So how do you pass args with uri_for? I was hoping to RTFM, but I
>> couldn't find the FM for "uri_for", and I decided to consult the
>> Oracle (this mailing list) before trouncing through Catalyst sourcecode.
>>
>> -d
>>
> 
> The only manual for anything that you might come across when using Catalyst  
> is the POD documentation itself and should be searched on search.cpan.org
> http://search.cpan.org/~jrockway/Catalyst-Manual-5.701002/lib/Catalyst/Manual/Cookbook.pod
> 
> 
> The values are passed as different arguments to uri_for.
> You could do $c->uri_for('readabook/read_book','$bid','$pid') .
> you can check the value returned by  using $c->debug->log..
> 
> Antano Solar John
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
> 



More information about the Catalyst mailing list