Zend Framework 是否有用于构建 URL 查询字符串的帮助程序?
我只是好奇&很困惑,因为我认为 Zend Framework 提供的工具几乎可以满足您构建 Web 应用程序时可能需要的所有内容。
在 Zend Framework 中,参数通常在 URL 中传递为 /param_name/param_value
,但如果我想构建一个以“?param_name=param_value¶m_name2=param_value2
”结尾的 URL,似乎我必须自己构建这个查询字符串并将其作为字符串传递,如下所示:
<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>
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('&', $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 http_build_query
Use http_build_query
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
如果您使用 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/