2
votes

I am using url based languages and i have a page with incoming parameters in URL.

I do not like query strings ('?x=y') or named params ('x:y') in URL so i use http://localhost/cakeapp/us/controller/action/param1/param2/param3

In my AppController i am checking if the URL has language defined, if no language is defined (for example the user requests http://localhost/cakeapp/controller/action/param1/param2/param3) then i want to simply redirect the user to the language defined URL with the same parameters.

In Short: I want to redirect:

http://localhost/cakeapp/controller/action/param1/param2/param3

to

http://localhost/cakeapp/us/controller/action/param1/param2/param3

I use;

$this->redirect(

Router::url(array('language' => $this->Session->read('Config.language'), 'base' => false))

);

But it redirects user to http://localhost/cakeapp/us/controller/action without parameters.

Is there a way to build url using Router::url with incoming $this->passedArgs variable.

2

2 Answers

0
votes

I think you need to ensure you pass the parameters to the redirect method:

function controllerAction($param1, $param2, $param3) {

    $this->redirect(

        Router::url(array('language' => $this->Session->read('Config.language'), 'base' => false)),
        $param1,
        $param2,
        $param3
    );

}

Or at least in some way pass your parameters into the redirect method, whichever way is most appropriate for you. I think you could do this too, if you are not passing the parameters directly as arguments into your controller action method:

$this->redirect(

    Router::url(array('language' => $this->Session->read('Config.language'), 'base' => false)),
    $this->request->params['named'][0],
    $this->request->params['named'][1],
    $this->request->params['named'][2]
);
0
votes

This should work:

Router::connect(
    '/cakeapp/:language/:controller/:action/*',
    array(), 
    array('language'=>'[a-z]{2}')
);
Router::redirect(
    '/*', 
    '/cakeapp/us/'.substr(Router::url(null, false), 
    strpos(Router::url(null, false), '/', 1))
);

You can leave out the string manipulation in the second row if you don't have an app prefix; like so:

Router::connect(
    '/:language/:controller/:action/*', 
    array(), 
    array('language'=>'[a-z]{2}')
);
Router::redirect('/*', '/us/'.Router::url(null, false));