在CakePHP中的URL上强制使用语言前缀 [英] Forcing language prefix on URL in CakePHP

查看:109
本文介绍了在CakePHP中的URL上强制使用语言前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制我的网站网址总是有一个语言后缀。

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

因此,如果他们键入 www.mysite.com 它应该带他们到 www.mysite.com/en

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

默认语言,因此,如果它是他们第一次到网站,应该使用。如果不是,我有一个Cookie被设置,我可以使用...但 - 我不知道在哪里使用它。

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.

我想检查看如果在URL中有一个语言参数,如果没有,重定向,但是 - 似乎过分 - 有没有更好的方法?我可以在路线中这样做吗?或引导?

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?

推荐答案

我做了什么:

我最终在AppController的beforeFilter()中检查是否设置了 $ this-> request-> params ['langauge'] ,因此构建URL:

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);
}

注意

我无法弄清楚的部分(直到在IRC中获得帮助)是使用 $ this-> request->这里,这只是作为字符串的URL。在此之前,我尝试使用params数组构建数组,但没有运气。

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.

我的路线(如果他们帮助任何人)

(请记住,我是一个路线noob,所以 - 虽然他们似乎在为我工作,我不保证他们做得很好) / p>

(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';

这篇关于在CakePHP中的URL上强制使用语言前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆