2

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);
}
Community
  • 1
  • 1
ANisus
  • 74,460
  • 29
  • 162
  • 158
  • 1
    Have you considered adding `lang` to your custom routes with a default lang if none is specified? – Crashspeeder May 17 '11 at 14:08
  • Yes, I have considered it. But I don't want the lang to be passed on every request, reverting back to the default if not. In my bootstrap I am checking if a lang is passed, if so I store it in the session. Also, it would be nice not having to add lang to every route. – ANisus May 18 '11 at 06:34
  • easiest way - store last request within session, add controller/action to switch language. It should redirect back to previous request fetched from session or to mainpage. – Xerkus May 19 '11 at 07:20

1 Answers1

1

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.
        }
    }
}
Xerkus
  • 2,695
  • 1
  • 19
  • 32
  • Yes, you are right. A custom route. Not router. I've edited and added one of the routes that fails to show my lang param. – ANisus May 18 '11 at 06:36
  • @ascherer yeah. I know at least two dozen ways to break route ) – Xerkus May 18 '11 at 12:21
  • 1
    Sorry for the delayed accept. Yes, your solution does work for me. Especially your addition. Thanks! – ANisus May 30 '11 at 19:31