Multilingual Routing Altorouter

Can I use Altorouter to create a multilingual routing configuration? I want to send a variable with the target file (so that it displays different contents when viewing), for example. -

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

But this fifth option is not available. Is there a workaround for this?

+6
source share
2 answers

You can use pattern matching in the URL to achieve this if 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 URL '/th/work/sample' . If you need more complex pattern matching, you can also specify custom regular expressions.

+4
source

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

But this fifth option is not available. Is there a workaround for this?

This is because the map function simply does not support the fifth parameter.

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

Source code AltoRouter.php: map

You can call the match function, which goes along the original route, if you can somehow intercept and make your code work up to the router. The match function returns the name of the route. But that means you create named routes for each language, and then you begin to understand the solution provided by @gbe

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

+2
source

All Articles