|
List Info
Thread: How to pass args/data from one controller to another?
|
|
| How to pass args/data from one
controller to another? |
  United States |
2007-09-28 21:43:27 |
So I've thought of a few different ways to pass data between
Local
methods across two different controllers, but none of them
seem like
good ideas. I still don't fully get the Catalyst framework
yet, so
I'm asking a very open question, "What are a few right
ways to do
this?" TIMTOWTDI, of course.
i.e.
Controller "Library" has a function
"list_books" that, duh, displays
a list of books in the lib. Each book is clickable creating
an
action in another function in Libarary called
"read_book" that's
only job in life as a function is to pass the $book_id over
to the
default action in the "ReadABook" controller. How
do you pass the
$book_id? Through the stash, though a path arg using
'uri_for' and a
redirect, using forward?
thanks,
-d
_______________________________________________
List: Catalyst lists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/
|
|
| Re: How to pass args/data from one
controller to another? |
  United States |
2007-09-29 01:39:37 |
Dustin Suchter wrote:
> So I've thought of a few different ways to pass data
between Local
> methods across two different controllers, but none of
them seem like
> good ideas. I still don't fully get the Catalyst
framework yet, so
> I'm asking a very open question, "What are a few
right ways to do
> this?" TIMTOWTDI, of course.
>
> i.e.
>
> Controller "Library" has a function
"list_books" that, duh, displays
> a list of books in the lib. Each book is clickable
creating an
> action in another function in Libarary called
"read_book" that's
> only job in life as a function is to pass the $book_id
over to the
> default action in the "ReadABook" controller.
How do you pass the
> $book_id? Through the stash, though a path arg using
'uri_for' and a
> redirect, using forward?
>
> thanks,
> -d
>
I'm a newbie with Catalyst as well, so hopefully if I say
anything too
incorrect, others will chime in. I think the best way to
handle the
book_id is simply to pass it via the URL. So in your
read_book
controller, you'd have something like:
my ($self, $c, $book_id) = _;
See the following page in the tutorial for passing arguments
via the URL:
http://searc
h.cpan.org/~jrockway/Catalyst-Manual-5.701002/lib/Catalyst/M
anual/Tutorial/BasicCRUD.pod
Hope that helps.
-Corey
_______________________________________________
List: Catalyst lists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/
|
|
| Re: How to pass args/data from one
controller to another? |
  Australia |
2007-09-29 01:49:21 |
On Saturday 29 Sep 2007 8:13:27 am Dustin Suchter wrote:
> So I've thought of a few different ways to pass data
between Local
> methods across two different controllers, but none of
them seem like
> good ideas. I still don't fully get the Catalyst
framework yet, so
> I'm asking a very open question, "What are a few
right ways to do
> this?" TIMTOWTDI, of course.
>
> i.e.
>
> Controller "Library" has a function
"list_books" that, duh, displays
> a list of books in the lib. Each book is clickable
creating an
> action in another function in Libarary called
"read_book" that's
> only job in life as a function is to pass the $book_id
over to the
> default action in the "ReadABook" controller.
How do you pass the
> $book_id? Through the stash, though a path arg using
'uri_for' and a
> redirect, using forward?
>
> thanks,
> -d
You can pass the arguments as arguments to uri_for .
It appends the value at the end of the uri prefixing a /,
The passed values are available in the same way variables
are passed to
functions inside the controller
Antano Solar John
_______________________________________________
List: Catalyst lists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/
|
|
| Re: How to pass args/data from one
controller to another? |
  United States |
2007-09-29 15:36:54 |
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
Antano Solar wrote:
> On Saturday 29 Sep 2007 8:13:27 am Dustin Suchter
wrote:
>> So I've thought of a few different ways to pass
data between Local
>> methods across two different controllers, but none
of them seem like
>> good ideas. I still don't fully get the Catalyst
framework yet, so
>> I'm asking a very open question, "What are a
few right ways to do
>> this?" TIMTOWTDI, of course.
>>
>> i.e.
>>
>> Controller "Library" has a function
"list_books" that, duh, displays
>> a list of books in the lib. Each book is clickable
creating an
>> action in another function in Libarary called
"read_book" that's
>> only job in life as a function is to pass the
$book_id over to the
>> default action in the "ReadABook"
controller. How do you pass the
>> $book_id? Through the stash, though a path arg
using 'uri_for' and a
>> redirect, using forward?
>>
>> thanks,
>> -d
>
> You can pass the arguments as arguments to uri_for .
> It appends the value at the end of the uri prefixing a
/,
> The passed values are available in the same way
variables are passed to
> functions inside the controller
>
> Antano Solar John
>
>
>
>
> _______________________________________________
> List: Catalyst lists.rawmode.org
> Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
> Dev site: http://dev.catalyst.per
l.org/
>
_______________________________________________
List: Catalyst lists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/
|
|
| Re: How to pass args/data from one
controller to another? |
  Australia |
2007-09-29 21:10:45 |
> 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/Cook
book.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 lists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/
|
|
| Re: How to pass args/data from one
controller to another? |
  United States |
2007-09-29 22:16:01 |
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:30
00/readabook', urlbroken1:
'http://server.co
m:3000/readabook/1/1', urlbroken2:
'http://server.com:3
000/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/Cook
book.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 lists.rawmode.org
> Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
> Dev site: http://dev.catalyst.per
l.org/
>
_______________________________________________
List: Catalyst lists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/
|
|
| Re: How to pass args/data from one
controller to another? |
  United States |
2007-09-29 22:38:10 |
On Friday 28 September 2007 09:43:27 pm Dustin Suchter
wrote:
> Controller "Library" has a function
"list_books" that, duh, displays
> a list of books in the lib. Each book is clickable
creating an
> action in another function in Libarary called
"read_book" that's
> only job in life as a function is to pass the $book_id
over to the
> default action in the "ReadABook" controller.
How do you pass the
> $book_id? Through the stash, though a path arg using
'uri_for' and a
> redirect, using forward?
I think we've been missing something in this entire
conversation, namely:
what's the point? Why have an action that does nothing
besides defer or
redirect or whatever to another action? Why does the first
one need to exist
in the first place?
Andrew
_______________________________________________
List: Catalyst lists.rawmode.org
Listinfo: ht
tp://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-
archive.com/catalyst lists.rawmode.org/
Dev site: http://dev.catalyst.per
l.org/
|
|
[1-7]
|
|