Pergunta

I was reading the CakePHP documentation on routing, but I can't get a grip on it yet.

What I want to achieve is this:

  • It should work with and without a language parameter in front of the controller parameter.
  • When an action is not given, it should go to the frontend action (this function also has input variables).

EDIT

In the end, I made my application logic that if there's only 1 language defined, there will be no lanugage prefix, and if there are more languages, the links will be generated with language prefix (by overriding the html helper) and the routing will be different. Otherwise there was always a problem when i tried to define the single language version routes (without a prefix). Here's my code that works now:

(app/Config/routes.php)

/*
 * ===================== Input start ===================== *
 */  

/**
 * Default language
 */
    Configure :: write('Config.language', 'mkd');

/**
 * Application languages
 */
    Configure :: write('Config.languages', array(
        'mkd' => 'Македонски', 
        'eng' => 'English',
    ));

/**
 * ====================== Input end ====================== *
 */ 



/**
 * Counting languages...
 */

Configure :: write('Config.languageCount', count(Configure :: read('Config.languages')));

/**
 * If application is multilingual
 */
    if(Configure :: read('Config.languageCount') > 1) {
        Router::connect('/:language/:controller/:action/*', 
            array(),
            array('language' => implode('|', array_keys( Configure :: read('Config.languages') )))
        );
        Router::connect('/:language/:controller/*', 
            array('action' => 'frontend'),
            array('language' => implode('|', array_keys( Configure :: read('Config.languages') )))
        );
    }
/**
 * If application has one language
 */
    else {
        Router::connect('/:controller/*', array('action' => 'frontend'));
    }

Thanks for the help Dave and kicaj, your help simplified my approach on this.

Foi útil?

Solução

You'll likely want to use regular expression instead - something like this:

Router::connect(
    '/:language/:controller/:action/*',
    array(),
    array('language'=>'[a-z]{3}'
));

Notice the third parameter which gives a name and a regex rule to :language.

It will then be available in your Controllers (you'll likely use in your AppController's beforeFilter()) with:

$this->request->params['language']
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top