Zend 框架:将查询字符串附加到当前页面 url [英] Zend Framework: Append query strings to current page url

查看:33
本文介绍了Zend 框架:将查询字符串附加到当前页面 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将查询字符串附加到 url?我当然可以做一个(来自控制器动作)

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

如果 requestUri 看起来像 /users 那很好.但是如果 url 看起来像 /users?page=1 呢?那么我最终会得到类似 /users?page=1/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

推荐答案

这不是向当前请求 URI 添加参数的可靠方法.例如,假设您使用的是默认模块路由,而您当前的 URI 是例如./新闻.如果你想在最后添加参数,你应该首先附加动作名称,因此有:/news/index/something/else.您可以看到手动执行此操作会变得非常乏味.Zend Framework 为您提供了轻松完成此操作的方法.在您的控制器中,您可以执行此操作以根据当前的 URI 生成一个 URI:

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

如果您想保留带有新 URI 的查询字符串,请执行以下操作:

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

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

这篇关于Zend 框架:将查询字符串附加到当前页面 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆