如何在 zend 框架中构建查询字符串?

发布于 2024-11-26 09:45:34 字数 609 浏览 1 评论 0原文

我正在尝试构建一个查询字符串,如下所示:

<a href="<?= $this->url(array('page' => $this->next)) ?>" class="next">Next Page</a>

我想向查询字符串添加一个数组。例如, array('find_loc'=>'New+York', 'find_name'=>'starbucks')

我希望得到看起来像 http://example.com/1/?find_loc=New+York&find_name=starbucks

执行此操作的最佳方法是什么?我发现了一个类似问题,建议附加字符串到网址。有查询字符串的助手吗?

I'm trying to build a query string as following:

<a href="<?= $this->url(array('page' => $this->next)) ?>" class="next">Next Page</a>

I want to add an array to query string. For example, array('find_loc'=>'New+York', 'find_name'=>'starbucks')

I expect to get url that looks like http://example.com/1/?find_loc=New+York&find_name=starbucks

What's the best way to do this? I found a similar question that suggested appending the string to the url. Is there a helper for query string?

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

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

发布评论

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

评论(4

冷弦 2024-12-03 09:45:34

你的问题的简单答案是否定的。

这是类描述:

/**
 * Helper for making easy links and getting urls that depend on the routes and router
 *
 * @package    Zend_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

用于创建简单链接并获取依赖路线路由器的网址

我认为描述很清楚这是目的。使用它来创建依赖于路由和路由器的 URL。因此,只需按照您在问题中发布的链接中的建议附加查询字符串即可。

Simple answer to your question is no.

Here is the class description:

/**
 * Helper for making easy links and getting urls that depend on the routes and router
 *
 * @package    Zend_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

Helper for making easy links and getting urls that depend on the routes and router

I think the description is clear in it's purpose. Use it for making URLs that depend on the routes and router. So, just append your query strings as recommend in the link you posted in your question.

守护在此方 2024-12-03 09:45:34

以下内容应该适合您:

<a href="<?= $this->url(array('page' => $this->next, 'find_loc' => 'New York', 'find_name' => 'starbucks')')) ?>" class="next">Next Page</a>

ZF- Router 会将这些值映射到 Request 对象。

在控制器中,您可以使用 访问这些参数响应对象

$loc  = $this->getRequest()->getParam('find_loc');
$name = $this->getRequest()->getParam('find_name); 

The following should work for you:

<a href="<?= $this->url(array('page' => $this->next, 'find_loc' => 'New York', 'find_name' => 'starbucks')')) ?>" class="next">Next Page</a>

The ZF-Router will map the values to the Request object.

In your controller you can access these params with the Response-Object:

$loc  = $this->getRequest()->getParam('find_loc');
$name = $this->getRequest()->getParam('find_name); 
国粹 2024-12-03 09:45:34

您可以制作自定义帮助器:

class My_View_Helper_UrlHttpQuery extends Zend_View_Helper_Abstract
{
    public function urlHttpQuery($query)
    {
        $urlHelper = $this->view->getHelper('url');
        $params = func_get_args();
        array_shift($params);//removing first argument
        $url = call_user_func_array(($urlHelper, 'url'), $params); 
        if(!is_string($query)) { //allow raw query string
            $query = array($query);
            $query = http_build_query($query);
        }
        if(!empty($query) {
            $url .= '?' . ltrim('?', $query);
        }
        return $url;
    }
}

在使用视图注册此帮助器后,您可以像这样使用它

You can make custom helper:

class My_View_Helper_UrlHttpQuery extends Zend_View_Helper_Abstract
{
    public function urlHttpQuery($query)
    {
        $urlHelper = $this->view->getHelper('url');
        $params = func_get_args();
        array_shift($params);//removing first argument
        $url = call_user_func_array(($urlHelper, 'url'), $params); 
        if(!is_string($query)) { //allow raw query string
            $query = array($query);
            $query = http_build_query($query);
        }
        if(!empty($query) {
            $url .= '?' . ltrim('?', $query);
        }
        return $url;
    }
}

After you register this helper with view, you can use it like this <a href="<?=$this->urlHttpQuery(array('find_loc'=>'New+York', 'find_name'=>'starbucks'), array('page' => $this->next), 'routename', $otherUrlHelperParams) ?>" class="next">Next Page</a>

纵性 2024-12-03 09:45:34

代码可以工作

/**
 * Class Wp_View_Helper_UrlHttpQuery
 */
class Wp_View_Helper_UrlHttpQuery extends Zend_View_Helper_Abstract
{
    public function urlHttpQuery($query = array())
    {
        $urlHelper = $this->view->getHelper('url');
        $params = func_get_args();
        //removing first argument
        array_shift($params);
        $url = call_user_func_array(array($urlHelper, 'url'), $params);
        if (is_array($query) || is_object($query)) {
            $query = http_build_query($query);
        }

        if (!empty($query)) {
            $url .= '?' . ltrim($query, '?');
        }
        return $url;
    }

}

由于上游代码不起作用,所以

Working code

/**
 * Class Wp_View_Helper_UrlHttpQuery
 */
class Wp_View_Helper_UrlHttpQuery extends Zend_View_Helper_Abstract
{
    public function urlHttpQuery($query = array())
    {
        $urlHelper = $this->view->getHelper('url');
        $params = func_get_args();
        //removing first argument
        array_shift($params);
        $url = call_user_func_array(array($urlHelper, 'url'), $params);
        if (is_array($query) || is_object($query)) {
            $query = http_build_query($query);
        }

        if (!empty($query)) {
            $url .= '?' . ltrim($query, '?');
        }
        return $url;
    }

}

since the upstream code doesn't work

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