Question

I am using Kohana 3.3

Goal

I would like to have my application select the proper view file based on the language in the URL. So for example:

mydomain.com/es/foobar would load the Spanish view file in my Foobar controller. This is working for anything but the base URL. Right now if you go to mydomain.com/es/ or mydomain.com/en/ I am getting a 404 returned. I would like for it to route to the Index controller at /classes/Controller/Index.php I am not sure what I am missing here. Any pointers would be appreciated.

NOTE:

mydomain.com correctly gets directed to the english language page.

I can post some Controller code if necessary but I am fairly certain this is just a routing problem.

Current Routes

/*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',
));
Was it helpful?

Solution

I replicated your problem and used your routes. After modifying them I came to the conclusion two routes would be easier. One for the normal URLs and one for the language routes.

Below are the routes I made:

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',
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top