Kohana Route问题到基本URL

时间:2013-07-22 18:09:55

标签: php kohana kohana-3 kohana-3.3

我正在使用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',
));

1 个答案:

答案 0 :(得分: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',
));