Zend Framework 是否有用于构建 URL 查询字符串的帮助程序?

发布于 2024-12-23 09:19:25 字数 1120 浏览 3 评论 0原文

我只是好奇&很困惑,因为我认为 Zend Framework 提供的工具几乎可以满足您构建 Web 应用程序时可能需要的所有内容。

在 Zend Framework 中,参数通常在 URL 中传递为 /param_name/param_value,但如果我想构建一个以“?param_name=param_value&param_name2=param_value2”结尾的 URL,似乎我必须自己构建这个查询字符串并将其作为字符串传递,如下所示:

<a href="<?php echo $this->url(array('controller'=>'index','action'=>'form'),NULL,TRUE)."?param_name=param_value&param_name2=param_value2";?>" title="">
    Some link
</a>

Zend Framework - 在我看来相当复杂 - 有一个帮助程序来构建此类查询字符串吗?我搜索了一下,但没有找到任何东西。

我正在寻找的是与此类似的东西,但更可靠:

function query_string(array $params) {
    $keys = array_keys($params);
    $vals = array_values($params);
    $build_param_func = create_function('$a,$b','return $a."=".$b;');
    $query_string_parts = array_map($build_param_func, $keys, $vals);
    return '?'.implode('&amp;', $query_string_parts);
};

请在此处查看其演示:http://ideone.com /piCNj

如果 Zend Framework 没有这样简单的助手,是否有任何模块提供它?或者完全由开发人员来创建这样的函数/助手?

感谢您的回答。

I am just curious & confused, because I considered Zend Framework as having tools for pretty much almost everything you may need when building a web application.

In Zend Framework parameters are usually passed within URL as /param_name/param_value, but if I want to build an URL ending with "?param_name=param_value¶m_name2=param_value2" it seems I have to build this query string myself and pass it as string like that:

<a href="<?php echo $this->url(array('controller'=>'index','action'=>'form'),NULL,TRUE)."?param_name=param_value¶m_name2=param_value2";?>" title="">
    Some link
</a>

Does Zend Framework - which was quite complex in my opinion - have a helper for building such query strings? I searched for that and did not find anything.

What I am looking for is something that works similarly to this, but more reliable:

function query_string(array $params) {
    $keys = array_keys($params);
    $vals = array_values($params);
    $build_param_func = create_function('$a,$b','return $a."=".$b;');
    $query_string_parts = array_map($build_param_func, $keys, $vals);
    return '?'.implode('&', $query_string_parts);
};

See its demo here: http://ideone.com/piCNj

If Zend Framework does not have such simple helper, is there any module that provides it? Or is it completely up to developer to create such function / helper?

Thanks for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

谎言月老 2024-12-30 09:19:25

PHP 有一个本机函数可以执行此操作,请参阅: http:// www.php.net/manual/en/function.http-build-query.php

PHP has a native function for doing this, see: http://www.php.net/manual/en/function.http-build-query.php

二手情话 2024-12-30 09:19:25

如果您使用 MVC,ZF 不会以传统方式使用查询字符串。

不过,您可以创建自己的助手:

http:// /blog.lysender.com/2009/04/creating-your-own-helper-zend-framework/

If you use MVC, ZF does not use query strings the traditional way.

You could create your own helper, though:

http://blog.lysender.com/2009/04/creating-your-own-helper-zend-framework/

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