I have a Catalyst Controller that responds to arguments in the usual way (/endpoint/foo/bar
), but also takes a query parameter for a security token. How can I set the value of the parameter before I initiate a forward
call to this Controller? It doesn't let me assign to $c->req->param('token')
.
(It's not possible to rewrite the controller to accept the token as an argument instead of a parameter.)
$c->req->param('token')
is not an LValue, so you cannot assign to it. Instead, you need to pass in the value.
$c->req->param('token', 1);
This behaviour is sort of documented:
Like CGI, and unlike earlier versions of Catalyst, passing multiple arguments to this method, like this:
$c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
will set the parameter foo to the multiple values bar, gorch and quxx. Previously this would have added bar as another value to foo (creating it if it didn't exist before), and quxx as another value for gorch.
Consider this demo:
package MyApp::Controller::Root;
use base 'Catalyst::Controller';
__PACKAGE__->config(namespace => '');
sub default : Path {
my ($self, $c) = @_;
$c->req->param('foo', 1);
$c->forward('bar');
}
sub bar : Path('foo') {
my ($self, $c) = @_;
if ($c->req->param('foo')) {
$c->res->body("Secret param set!\n");
}
else {
$c->res->body("Hello World\n");
}
return;
}