views:

27

answers:

1

how can i append query strings to a url? i could of course do a (from controller action)

$currUrl = $this->getRequest()->getRequestUri();
$newUrl = $currUrl . '/something/else';

if the requestUri looks like /users thats fine. but what if the url looks like /users?page=1? then i will end up with something like /users?page=1/something/else which is wrong

+2  A: 

That is not a reliable way to add parameters to the current request URI. Say for example that you're using the default module route, and your current URI is eg. /news. If you want to add params to the end, you should first append the action name, hence having: /news/index/something/else. You can see that it can become quite tedious to do this by hand. Zend Framework provides you methods to do this with ease. In your controller, you can do this to generate an URI based on the current one:

$router = Zend_Controller_Front::getInstance()->getRouter();
$url = $router->assemble(array('something' => 'somethingelse'));

If you want to keep the query string with the new URI, do after that:

if (!empty($_SERVER['QUERY_STRING']))
    $url .= '?'.$_SERVER['QUERY_STRING'];
reko_t