Forcing language prefix on URL in CakePHP

2019-06-13 17:54发布

问题:

I'd like to force my site's URL to always have a language suffix.

So, if they type www.mysite.com it should take them to www.mysite.com/en.

I have a default language, so that should be used if it's their first time to the site. If it's not, I have a Cookie being set that I can use...but - I don't know where to use it.

I thought about checking to see if there was a "language" parameter in the URL, then if not, redirecting, but - that seems overkill - is there a better way? Can I do this in routes? or bootstrap?

回答1:

The most efficient way would be through your web server. You can easily check if the request is for / (the home page) and redirect to /en.

Check the docs for what ever web server you are using, they all have something like mod_rewrite or similar.

Edit

You could set up a route like /set_default_language to redirect to in case of /, this controller can access the db and do what ever it needs.

Alternatively you can make it redirect to /your/usual/language_switch with no language specified and allow the code to use the default.



回答2:

What I did:

I ended up checking in the AppController's beforeFilter() whether or not $this->request->params['langauge'] was set and if not, building the URL accordingly:

//Redirect to same url, but with language parameter
if (empty($this->request->params['language']) &&
empty($this->request->params['admin'])) {
    $defaultLanguageCode = Configure::read('Languages.default.code2');
    $cookiedLanguage = $this->Language->activeLanguageByCode($this->Cookie->read('lang'));
    $languageToRedirectTo = (!empty($cookiedLanguage['code2'])) ? cookiedLanguage['code2'] : $defaultLanguageCode;
    $newURL = '/' . $languageToRedirectTo . $this->request->here;
    $this->redirect($newURL);
}

Note:

The part I couldn't figure out (until getting help in IRC) was to build the URL using $this->request->here, which is just the URL as a string. Prior to that I tried building out the array using the params array, but had no luck.

My routes (in case they help anyone)

(Keep in mind, I'm a routes noob, so - although they seem to be working for me, I do NOT guarantee they're done well!)

//root URL and root URL w/ language
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));  // eg: www.google.com takes them to pages/display/home
Router::connect('/:language', array('controller'=>'pages', 'action' => 'display', 'home'), array('language'=>'[a-z]{2}')); // eg: /en takes them to pages/display/home and sets language

//pages (eg. /en/r/the_matrix  or  /r/the_matrix)
Router::connect('/:language/r/:slug/*', array('controller'=>'pages', 'action'=>'display'), array('language'=>'[a-z]{2}', 'pass'=>array('slug')));
Router::connect('/r/:slug/*', array('controller'=>'pages', 'action'=>'display'), array('pass'=>array('slug')));

//adds language to default URL
Router::connect('/:language/:controller/:action/*', array(), array('language'=>'[a-z]{2}'));

//Route prefixes
Configure::write('Routing.prefixes', array('admin'));

//User related
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/myaccount', array('controller' => 'users', 'action' => 'my_account'));

//
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';


回答3:

If you don't want to redirect the requestAction calls from within your controllers or views you should add the following condition to your IF statement

    if (empty($this->request->params['language']) &&
    empty($this->request->params['admin']) && 
    empty($this->request->params['requested'])) {
    ...
    }