CakePHP中的本地化路由:如何重定向到默认语言

eme*_*his 7 php routing localization cakephp

自2012年以来,这篇文章似乎是关于如何在CakePHP中进行本地化路由的最权威资源(下面复制的代码).

它工作得很好,但有一个例外:它不会重定向缺少语言前缀的请求.例如,http://example.com将显示与http://example.com/eng相同的内容(如果英语是默认语言).同样,如果它不是主页:http://example.com/foo/bar/ => http://example.com/eng/foo/bar.在评论中有一些提到这个问题,但没有决定性的解决方案,这正是我正在寻找的.

码.

// Step 1: app/Config/routes.php
Router::connect('/:language/:controller/:action/*',
                  array(),
                  array('language' => 'eng|fra'));

Router::connect('/:language/:controller',
                  array('action' => 'index'),
                  array('language' => 'eng|fra'));  

Router::connect('/:language',
                  array('controller' => 'welcome', 'action' => 'index'),
                  array('language' => 'eng|fra'));

//Step 2: app/Config/core.php
Configure::write('Config.language', 'eng');

//Step 3: create app/View/Helper/MyHtmlHelper.php
App::uses('HtmlHelper', 'View/Helper');
class MyHtmlHelper extends HtmlHelper {
    public function url($url = null, $full = false) {
        if(!isset($url['language']) && isset($this->params['language'])) {
          $url['language'] = $this->params['language'];
        }
        return parent::url($url, $full);
   }
}

//Step 4: app/Controller/AppController.php
class AppController extends Controller {
    public $components = array('Cookie','Session');
    //set an alias for the newly created helper: Html<->MyHtml
    public $helpers = array('Html' => array('className' => 'MyHtml'));

    public function beforeFilter() {
          $this->_setLanguage();
        }

    private function _setLanguage() {
    //if the cookie was previously set, and Config.language has not been set
    //write the Config.language with the value from the Cookie
        if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
            $this->Session->write('Config.language', $this->Cookie->read('lang'));
        } 
        //if the user clicked the language URL 
        else if (   isset($this->params['language']) && 
        ($this->params['language'] !=  $this->Session->read('Config.language'))
                ) {
            //then update the value in Session and the one in Cookie
            $this->Session->write('Config.language', $this->params['language']);
            $this->Cookie->write('lang', $this->params['language'], false, '20 days');
        }
    }

    //override redirect
    public function redirect( $url, $status = NULL, $exit = true ) {
        if (!isset($url['language']) && $this->Session->check('Config.language')) {
            $url['language'] = $this->Session->read('Config.language');
        }
        parent::redirect($url,$status,$exit);
    }
}

//add the links to the languages:
//Step 5: app/View/...
echo $this->Html->link('English', array('language'=>'eng')); 
echo $this->Html->link('Français', array('language'=>'fra')); 
Run Code Online (Sandbox Code Playgroud)

更新1

我尝试了user221931的建议,但似乎没有用.这是我添加到我的路线中的内容:

/* Add default language param */
Router::redirect('/:controller/:action/*', 
                    array('language' => 'fra'), 
                    array('persist' => false) );
Router::redirect('/:controller/', 
                    array('language' => 'fra'), 
                    array('persist' => false) );
Router::redirect('/', 
                    array('controller'=>'pages', 'action'=>'display', 'language' => 'fra', 'home'), 
                    array('persist' => false) );
Run Code Online (Sandbox Code Playgroud)

它似乎没有效果.以下网址重定向:http:example.com/,http: //example.com/controller/,http : //example.com/controller/action/

更新2 根据要求,这是我的完整路线文件:

<?php
/**
 * Routes configuration
 *
 * In this file, you set up routes to your controllers and their actions.
 * Routes are very important mechanism that allows you to freely connect
 * different URLs to chosen controllers and their actions (functions).
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Config
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

#http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
Router::parseExtensions('json');

/**
 * Here, we are connecting '/' (base path) to controller called 'Pages',
 * its action called 'display', and we pass a param to select the view file
 * to use (in this case, /app/View/Pages/home.ctp)...
 */
    Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
 * ...and connect the rest of 'Pages' controller's URLs.
 */
    Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));


/**
 * LOCALIZED URLs
 * See: http://colorblindprogramming.com/multiple-languages-in-a-cakephp-2-application-in-5-steps
 */
Router::connect('/:language/:controller/:action/*',
                       array(),
                       array('language' => 'eng|fra'));

Router::connect('/:language/:controller',
                   array('action' => 'index'),
                   array('language' => 'eng|fra')); 

Router::connect('/:language',
                   array('controller' => 'pages', 'action' => 'display', 'home'),
                   array('language' => 'eng|fra'));

# prevent routing conflicts with plugins...
# http://www.omaroid.com/cakephp-locale-language-routing/
// make an array of loaded plugins
$loaded = CakePlugin::loaded();
array_walk($loaded, function(&$item,$key){
    $item = Inflector::underscore($item);
});
$loaded = implode('|', $loaded);

Router::connect('/:language/:plugin/:controller/:action/*', 
                    array(), 
                    array('language' => 'eng|fra','plugin' => "($loaded)"));

/* HIDE /index */
//Router::connect('/:controller/', array('action'=>'index') );
Router::connect('/:language/:controller/', array('action'=>'index') );

/**
 * Load all plugin routes. See the CakePlugin documentation on
 * how to customize the loading of plugin routes.
 */
    CakePlugin::routes();

/**
 * Load the CakePHP default routes. Only remove this if you do not want to use
 * the built-in default routes.
 */
    require CAKE . 'Config' . DS . 'routes.php';
Run Code Online (Sandbox Code Playgroud)

use*_*931 1

您添加了非常具体的语言路由(如果 url 路径的第一部分是engfra),但没有删除默认路由 ( Router::connect('/:controller/:action/*'),因此如果找不到这两个单词,则更通用的路由/:controller/:action/*将匹配。

您可以执行以下三件事中的任意一项:

  1. 删除默认路由,这将导致/:controller/:action/*不可/:controller/*路由,并且没有人能够形成能够访问没有语言的内容的 URL。
  2. 添加routes重定向到engie /users-> /eng/users/posts/->/eng/posts等的硬编码。这看起来工作量很大,但是虽然模式匹配对于开发来说是可以的,但硬编码可以极大地提高路由速度,因此您可能需要在将来的某个时候这样做。它的另一个优点是您可以添加一条persist规则,使浏览器记住重定向并且不再访问服务器。
  3. 在您的代码中添加代码,AppController检查路径中是否包含语言,如果不包含,请尝试从浏览器设置中猜测用户语言,以便重定向到最合适的网址或默认网址。

第 1 种方法是最简单、最干净且最快的方法,但如果您的控制器不需要前面有语言,则可能不合适。

第二点不允许您选择重定向到的语言,因为它将被硬编码。根据应用程序的不同,它可能完全没问题。

从 UX 角度来看,第 3 种解决方案是最优雅的解决方案,但在每个页面加载中会造成一些轻微的开销(通过检查语言)。