Zend Framework: Append query strings to current page url

让人想犯罪 __ 提交于 2020-01-15 09:33:53

问题


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


回答1:


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'];


来源:https://stackoverflow.com/questions/3519360/zend-framework-append-query-strings-to-current-page-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!