2

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?

Community
  • 1
  • 1
Vlad Vinnikov
  • 1,457
  • 3
  • 22
  • 33

4 Answers4

1

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.

brady.vitrano
  • 2,256
  • 2
  • 16
  • 26
0

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

Farkhod Daniyarov
  • 748
  • 12
  • 14
0

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); 
Benjamin Cremer
  • 4,842
  • 1
  • 24
  • 30
  • This didn't work for me as url helper matches variables with a router structure. In other words, it only works if I have variables predefined in application.ini - resources.router.routes.home.route = /:page/:find_name/:find_loc – Vlad Vinnikov Jul 22 '11 at 18:23
0

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>

Xerkus
  • 2,695
  • 1
  • 19
  • 32