路由和 URL 参数 - ZendFramework
我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个示例基于默认路由,如下所示
:module/:controller/:action/*
注意路线末尾的
*
;它定义 url 可以包含额外的键/值对。要使您的联系路线正常工作,您可以使用
它使网址看起来像
/contact/pt
。或者您可以使用:这将导致
/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
this will make the url look like
/contact/pt
. Or you can use:Which will result in
/contact/index/lang/pt
您也可以使用以下代码:
Zend_Controller_Front
Zend_Controller_Router_Route
Also you can use this code:
Zend_Controller_Front
Zend_Controller_Router_Route