router
router copied to clipboard
Get parameters in mount function
My domains are structured like this to server different language versions of the same page:
example.com/nl/page-1
and example.com/en/page-1
for the same url in different languages.
My code looks like this:
$router->mount('/(\w+)', function($lang) use ($router){ // wrap in lang-var
echo $lang;
$router->get('/page-1', function() {
// code
});
$router->get('/page-2', function() {
// code
});
});
But it gives me the following error:
Warning: Missing argument 1 for {closure}() in [path]/routes.php on line 13
Is there a way to get the baseroute in a variable here?
Ya, this router only accepts static options in mount(). Doesn't support regex. It needs improvement.
Here is what you can do instead :
$router->mount('/(\w+)', function() use ($router) {
$router->get('/(\w+)', function($lang, $page) {
dump($lang, $page);
});
});
$router->run();
URL: /en/account
Please check the included multilang demo files, as they provide what you're willing to do.
As for mount()
itself: I'll gladly accept a PR that provides this.