使用 Zend 中的视图助手将参数附加到当前 URL

发布于 2024-11-07 18:10:21 字数 1486 浏览 5 评论 0原文

在我的布局脚本中,我希望创建一个链接,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2024-11-14 18:10:21

更新:

您必须在路由中添加通配符或显式定义“lang”参数。

'admin/user/:mode/:id/*'

此外,根据您的评论,您可以执行以下操作:

class controllerplugin extends Zend_Controller_Plugin_Abstract
{
    function routeShutdown($request)
    {
        if($request->getParam('lang', false) {
            //store lang
            Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('lang', NULL); //check if this will remove lang from wildcard parameters, have no working zf installation here to check.
        }
    }
}

Upd:

You must add wildcard to your route or define 'lang' parameter explicitly.

'admin/user/:mode/:id/*'

Additionally, according to your comment, you can do something like this:

class controllerplugin extends Zend_Controller_Plugin_Abstract
{
    function routeShutdown($request)
    {
        if($request->getParam('lang', false) {
            //store lang
            Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('lang', NULL); //check if this will remove lang from wildcard parameters, have no working zf installation here to check.
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文