router icon indicating copy to clipboard operation
router copied to clipboard

Get parameters in mount function

Open Duncank opened this issue 6 years ago • 3 comments

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?

Duncank avatar Aug 15 '18 08:08 Duncank

Ya, this router only accepts static options in mount(). Doesn't support regex. It needs improvement.

abmmhasan avatar Sep 05 '18 09:09 abmmhasan

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 capture

artyuum avatar Dec 15 '18 12:12 artyuum

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.

bramus avatar Jan 25 '19 22:01 bramus