在 Perl/Catalyst 中通过 POST 实现多维和关联数组(哈希)

发布于 2024-11-27 19:32:52 字数 468 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2024-12-04 19:32:52

这不是开箱即用的 - Catalyst 不会对参数名称进行任何映射,并且将值映射到标量(如果它们出现一次)或 arrayrefs(如果它们出现多次)。但是,您可以应用一个请求特征, Catalyst::TraitFor ::Request::Params::Hashed 添加了 hashed_pa​​ramshashed_query_paramshashed_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, and hashed_body_params methods to $c->request that behave in pretty much the way you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文