douyan9417
2011-12-15 03:30
浏览 21
已采纳

路由和URL参数 - ZendFramework

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

图片转代码服务由CSDN问答提供 功能建议

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

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

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

没有路线它可以正常工作。 访问localhost / app / contact,我得到了正确的链接Ex:localhost / app / contact / index / lang / en

但是如果我添加路由 \ n

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

我得到没有lang param的链接。 例如:localhost / app / contact /

我如何解决这个问题?

谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutang6130 2012-01-06 20:53
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dseigqk7443 2012-10-10 11:23

    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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题