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?

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you saying that this is the best way do it: <a href="<?= $this->url(array('page' => $this->next))."?".http_build_query(array('find_loc'=>'New+York', 'find_name'=>'starbucks')) ?>" class="next">Next Page </a>
Absolutely. Very common practice when building searches.
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); 

1 Comment

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
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>

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.