Search code examples
routesmultilingualaltorouter

Altorouter multilingual routing


Is it possible to use Altorouter to create a multilingual routing setup? I'd want to send a variable with the target file (so that it displays different contents when viewed), e.g. -

$router->map('GET','/th/work/sample', 'work/sample.php', 'sample', 'th');

But that fifth parameter isn't available. Is there any workaround for this?


Solution

  • You can use pattern-matching in the URL to achieve this, if the your language URLs are regular enough.

    According to the documentation, defining the route

    $router->map('GET', '/[:lang]/work/sample', 'work/sample.php', 'sample')
    

    will capture 'th' in $lang when the '/th/work/sample' URL is hit. If you need more complex pattern matching, custom regexes can be also be specified.