2

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&param_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&param_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('&amp;', $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.

0

3 Answers 3

3

Use http_build_query

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

Comments

3

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

1 Comment

Thank you very much! Did not expect PHP itself to provide this function ;)
2

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/

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.