使用 Zend 中的视图助手将参数附加到当前 URL
在我的布局脚本中,我希望创建一个链接,使用 url 视图助手将 [?|&]lang=en 附加到当前 url。所以,我希望这种情况发生:
http://localhost/user/edit/1 => http://localhost/user/edit/1?lang=en
http://localhost/index?page=2 => http://localhost/index?page=2&lang=en
我已经尝试过建议的解决方案 Zend Framework:将查询字符串附加到当前页面 url< /a>,直接访问路由器,但这确实有效。
我的布局脚本包含:
<a href="<?=$this->escape( $this->url( array('lang'=>'en')) )?>">English</a>
如果使用默认路由,它将附加/lang/en,但是当使用另一个路由时,根本不会附加任何内容。
那么,有没有什么方法可以在 Zend 中做到这一点,而无需我解析 url?
编辑
抱歉我的解释有误。不,我还没有制作定制路由器。我刚刚添加了其他路线。我的不好。一条不起作用的路线是:
$Routes = array(
'user' => array(
'route' => 'admin/user/:mode/:id',
'defaults' => array('controller' => 'admin', 'action' => 'user', 'mode'=>'', 'id' => 0)
)
);
foreach( $Routes as $k=>$v ) {
$route = new Zend_Controller_Router_Route($v['route'], $v['defaults']);
$router->addRoute($k, $route);
}
In my layout-script I wish to create a link, appending [?|&]lang=en to the current url, using the url view helper. So, I want this to happen:
http://localhost/user/edit/1 => http://localhost/user/edit/1?lang=en
http://localhost/index?page=2 => http://localhost/index?page=2&lang=en
I have tried the solution suggested Zend Framework: Append query strings to current page url, accessing the router directly, but that does work.
My layout-script contains:
<a href="<?=$this->escape( $this->url( array('lang'=>'en')) )?>">English</a>
If the default route is used, it will append /lang/en, but when another route is used, nothing is appended at all.
So, is there any way to do this with in Zend without me having to parse the url?
Edit
Sorry for my faulty explanation. No, I haven't made a custom router. I have just added other routes. My bad. One route that doesn't work is:
$Routes = array(
'user' => array(
'route' => 'admin/user/:mode/:id',
'defaults' => array('controller' => 'admin', 'action' => 'user', 'mode'=>'', 'id' => 0)
)
);
foreach( $Routes as $k=>$v ) {
$route = new Zend_Controller_Router_Route($v['route'], $v['defaults']);
$router->addRoute($k, $route);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
您必须在路由中添加通配符或显式定义“lang”参数。
此外,根据您的评论,您可以执行以下操作:
Upd:
You must add wildcard to your route or define 'lang' parameter explicitly.
Additionally, according to your comment, you can do something like this: