路由和 URL 参数 - ZendFramework

发布于 2024-12-21 10:30:44 字数 1373 浏览 1 评论 0原文

我遇到了 zendframework 路由和参数的问题。

我的视图页面中有语言选择器:

 <div class="language-chooser">
    <?
    $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
    unset($params['module']);
    unset($params['controller']);
    unset($params['action']);
    ?>
    <a href="<?= $this->url(array_merge($params, array('lang' => 'pt'))); ?>"><img src="<?= $this->baseUrl('/images/flags/br.png'); ?>" alt="" /></a>
    <a href="<?= $this->url(array_merge($params, array('lang' => 'en'))); ?>"><img src="<?= $this->baseUrl('/images/flags/us.png'); ?>" alt="" /> </a>
</div>

无需路由即可正常工作。访问 localhost/app/contact,我正确地获得了链接 例如: localhost/app/contact/index/lang/en

但是,如果我添加一条路线

protected function _initRotas() {
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $route = new Zend_Controller_Router_Route(
                    '/contact',
                    array(
                        'module' => 'default',
                        'controller' => 'contact',
                        'action' => 'index'
                    )
    );
    $router->addRoute('contact', $route);
}

,我会获得没有 lang 参数的链接。例如:localhost/app/contact/

我该如何解决这个问题?

谢谢

I'm having an issue with zendframework routes and params.

I have language selector in my view page:

 <div class="language-chooser">
    <?
    $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
    unset($params['module']);
    unset($params['controller']);
    unset($params['action']);
    ?>
    <a href="<?= $this->url(array_merge($params, array('lang' => 'pt'))); ?>"><img src="<?= $this->baseUrl('/images/flags/br.png'); ?>" alt="" /></a>
    <a href="<?= $this->url(array_merge($params, array('lang' => 'en'))); ?>"><img src="<?= $this->baseUrl('/images/flags/us.png'); ?>" alt="" /> </a>
</div>

It works fine without routes. Accessing localhost/app/contact, I get the link correctly Ex.: localhost/app/contact/index/lang/en

But if I add a route

protected function _initRotas() {
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $route = new Zend_Controller_Router_Route(
                    '/contact',
                    array(
                        'module' => 'default',
                        'controller' => 'contact',
                        'action' => 'index'
                    )
    );
    $router->addRoute('contact', $route);
}

I get the link without the lang param. Ex.: localhost/app/contact/

How could i solve this issue?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情徒 2024-12-28 10:30:44

第一个示例基于默认路由,如下所示 :module/:controller/:action/*
注意路线末尾的 * ;它定义 url 可以包含额外的键/值对。

要使您的联系路线正常工作,您可以使用

$route = new Zend_Controller_Router_Route(
    '/contact/:lang',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    )
);

它使网址看起来像 /contact/pt。或者您可以使用:

$route = new Zend_Controller_Router_Route(
    '/contact/*',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    )
);

这将导致 /contact/index/lang/pt

The first example is based on the default route, which looks like :module/:controller/:action/*
Notice the * at the end of the route; it defines that the url can contain additional key/value pairs.

To make your contact route work, you could either use

$route = new Zend_Controller_Router_Route(
    '/contact/:lang',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    )
);

this will make the url look like /contact/pt. Or you can use:

$route = new Zend_Controller_Router_Route(
    '/contact/*',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    )
);

Which will result in /contact/index/lang/pt

心舞飞扬 2024-12-28 10:30:44

您也可以使用以下代码:

$Router = Zend_Controller_Front::getInstance()->getRouter();
$Router->addRoute('move', new Zend_Controller_Router_Route(
    '/contact/:lang',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    ),
    array(
        'lang' => '[a-z]+'
    )
));

Zend_Controller_Front

Zend_Controller_Router_Route

Also you can use this code:

$Router = Zend_Controller_Front::getInstance()->getRouter();
$Router->addRoute('move', new Zend_Controller_Router_Route(
    '/contact/:lang',
    array(
        'module' => 'default',
        'controller' => 'contact',
        'action' => 'index'
    ),
    array(
        'lang' => '[a-z]+'
    )
));

Zend_Controller_Front

Zend_Controller_Router_Route

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文