0

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

dextervip
  • 4,999
  • 16
  • 65
  • 93
  • Read this http://stackoverflow.com/questions/8345018/zend-reverse-matching-of-routes-returns-current-url/ – Raj Dec 15 '11 at 06:48

2 Answers2

2

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

Pieter
  • 1,764
  • 1
  • 12
  • 16
  • "Notice the `*` at the end of the route; it defines that the url can contain additional key/value pairs." => this is what made me crazy. I needed the star... THX !!! – LittleBigDev Oct 22 '12 at 03:20
0

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

RDK
  • 4,540
  • 2
  • 20
  • 29