0

我正在使用 Kohana 3.3

目标

我想让我的应用程序根据 URL 中的语言选择正确的视图文件。例如:

mydomain.com/es/foobar将在我的 Foobar 控制器中加载西班牙语视图文件。这适用于除基本 URL 之外的任何内容。现在,如果你去,mydomain.com/es/或者mydomain.com/en/我收到 404 退货。我希望它在/classes/Controller/Index.php我不确定我在这里缺少什么时路由到索引控制器。任何指针将不胜感激。

笔记:

mydomain.com正确地被定向到英语页面。

如有必要,我可以发布一些控制器代码,但我相当确定这只是一个路由问题。

当前路线

/*I don't think this one is even getting fired even though it's first */
Route::set('mydomain_home', '(<lang>/)',
array(
    'lang' => '(en|es)'
))
->filter(function($route, $params, $request)
{   
    $lang = is_empty($params['lang']) ? 'en' : $params['lang'];
    /*This debug statement is not printing*/
    echo Debug::vars('Language in route filter: '.$lang);
    $params['controller'] = $lang.'_Index';
    return $params; // Returning an array will replace the parameters
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));;

/*This one works for the non-base URL e.g. mydomain.com/es/page1 */
Route::set('mydomain_default', '(<lang>/)(<controller>(/<action>(/<subfolder>)))',
array(
    'lang' => '(en|es)',
))
->filter(function($route, $params, $request) {
        // Replacing the hyphens for underscores.
        $params['action'] = str_replace('-', '_', $params['action']);
        return $params; // Returning an array will replace the parameters.
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));
4

1 回答 1

1

我复制了您的问题并使用了您的路线。在修改它们之后,我得出的结论是两条路线会更容易。一个用于普通 URL,一个用于语言路由。

以下是我制作的路线:

Route::set('language_default', '(<lang>(/<controller>(/<action>(/<subfolder>))))',
array(
    'lang' => '(en|es)',
))
->filter(function($route, $params, $request) {
    // Replacing the hyphens for underscores.
    $params['action'] = str_replace('-', '_', $params['action']);
    return $params; // Returning an array will replace the parameters.
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));


Route::set('default', '(<controller>(/<action>(/<subfolder>)))')
->filter(function($route, $params, $request) {
    // Replacing the hyphens for underscores.
    $params['action'] = str_replace('-', '_', $params['action']);
    return $params; // Returning an array will replace the parameters.
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));
于 2013-07-23T13:43:04.960 回答