6

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?

Staffan Estberg
  • 6,795
  • 16
  • 71
  • 107

2 Answers2

4

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.

gbe
  • 671
  • 3
  • 11
2

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

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

That is because the map function just does not support a fifth parameter.

public function map($method, $route, $target, $name = null)

source code AltoRouter.php:map

You can call match function passing in the original route if you can somehow intercept and get your code to work before router. The match function returns you a route name. But this means you create named routes for each language and then you will begin to appreciate the solution provided by @gbe

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

bhantol
  • 9,368
  • 7
  • 44
  • 81