如何让 Zend_Controller_Router 默认重置不需要的参数
我正在使用 Zend-Framework 1.9.5 制作一个 Web 应用程序,但它的 Url_Helper 在参数重置方面对我来说相当棘手!,我知道这是一个很好的功能(参数保留),但在大多数情况下我不这样做不需要它! 所以我正在考虑覆盖默认路由器以强制它丢失参数,除非我要求它或者指定它保留的某些参数(lang,或类似的东西)。
另外,我想将其设为默认路由器,这样我就不必编辑我的控制器、视图来完成此任务!
有什么建议吗?
更新: 我花了整个上午尝试编写我的 url 帮助器 Admin_View_Helper_Xurl
,但我无法做任何解决问题的事情:
<?php
class Admin_View_Helper_Xurl extends Zend_View_Helper_Abstract
{
public function xurl(array $urlOptions = array(), $name = 'default', $reset = false, $encode = true)
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$wanted_params = array('module', 'controller', 'action', 'lang', 'page', 'search');
$route = $router->getCurrentRoute();
$something = anyWayToGetThatObjectOrClass();
$params = $something->getParams();
foreach($params as $key => $val) {
if (!in_array($key, $wanted_params)) {
$params[$key] = null; // OR uset($params[$key]);
}
}
$something->clearParams();
$something->setParams($params);
return $router->assemble($urlOptions, $name, $reset, $encode);
}
}
我尝试获取当前的 URL 参数并过滤它们并清除当前参数并传递我的过滤了,但如果不硬编码编辑一个 Zend_Framework 代码,我就无法做任何事情:(。
谢谢
I'm using Zend-Framework 1.9.5 to make a web-application, But it's Url_Helper was quite tricky to me in the matter of parameter reset!, I know it's a good feature (parameter preserving) but in most cases I don't need it!.
So I'm thinking of overriding the default Router to force it loosing parameters Unless I ask for it or maybe specifying a certain parameters that it keeps like (lang, or something like that).
Also I want to make it the default router so I don't have to edit my Controllers, Views to get that done!
Any suggestions?
Update:
I spent the whole morning trying to write my url helper Admin_View_Helper_Xurl
, But I couldn't do anything that solves the problem:
<?php
class Admin_View_Helper_Xurl extends Zend_View_Helper_Abstract
{
public function xurl(array $urlOptions = array(), $name = 'default', $reset = false, $encode = true)
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$wanted_params = array('module', 'controller', 'action', 'lang', 'page', 'search');
$route = $router->getCurrentRoute();
$something = anyWayToGetThatObjectOrClass();
$params = $something->getParams();
foreach($params as $key => $val) {
if (!in_array($key, $wanted_params)) {
$params[$key] = null; // OR uset($params[$key]);
}
}
$something->clearParams();
$something->setParams($params);
return $router->assemble($urlOptions, $name, $reset, $encode);
}
}
I tried to get current URL parameters and filter them and clear the current parameters and pass my filtered ones but I couldn't do anything that does it without hard-code editing one Zend_Framework code :(.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当生成链接视图时,您可以要求助手使用简单的布尔值删除所有参数:
最后一个参数告诉是否重置参数。
When generating a link a view, you can ask the helper to get rid of all aparamters with a simple boolean :
The last parameter tells whether to reset parameters or not.
我想出了这个解决方案。花了7个小时才发挥作用。
显然,它是一个 View Helper 类,可能不是最好的解决方案,但目前对我来说效果很好。
I came up with this solution. It took 7 hours to be functional.
Clearly it's a View Helper class, may be not the best solution but it works fine with me for now.