在 Perl/Catalyst 中通过 POST 实现多维和关联数组(哈希)
在 PHP 中,我会这样做来获取“点”作为多维数组。
<input type"text" name="points[0][1]" />
<input type"text" name="points[0][2]" />
<input type"text" name="points[1][1]" />
<input type"text" name="points[2][2]" />
或者,如果我想将“点”作为关联数组:
<input type"text" name="point[x_axis]" />
<input type"text" name="point[y_axis]" />
对于此类事物,Catalyst/Perl 的等效项是什么? 我如何通过 $c->req->params 获得这些?
In PHP, I would do this to get 'points' as an multidimensional array.
<input type"text" name="points[0][1]" />
<input type"text" name="points[0][2]" />
<input type"text" name="points[1][1]" />
<input type"text" name="points[2][2]" />
Or if I wanted to get 'point' as an associative array:
<input type"text" name="point[x_axis]" />
<input type"text" name="point[y_axis]" />
What is the Catalyst/Perl equivalent for such things?
How can I get these through $c->req->params ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是开箱即用的 - Catalyst 不会对参数名称进行任何映射,并且将值映射到标量(如果它们出现一次)或 arrayrefs(如果它们出现多次)。但是,您可以应用一个请求特征, Catalyst::TraitFor ::Request::Params::Hashed 添加了
hashed_params
、hashed_query_params
和hashed_body_params
方法到$c->request
的行为几乎符合您想要的方式。This isn't provided out-of-the-box — Catalyst doesn't do any mapping on param names, and maps the values to either scalars if they appear once, or arrayrefs if they appear multiple times. However there's a request trait you can apply, Catalyst::TraitFor::Request::Params::Hashed that adds
hashed_params
,hashed_query_params
, andhashed_body_params
methods to$c->request
that behave in pretty much the way you want.