Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to group links with optional parameter #58

Open
SlamDevelop opened this issue Oct 7, 2021 · 8 comments
Open

How to group links with optional parameter #58

SlamDevelop opened this issue Oct 7, 2021 · 8 comments
Labels

Comments

@SlamDevelop
Copy link

For grouping I use the pattern, but the problem is that everything works only when the url is specified with the language, without the language it gives an error.

These links work

  • test.com/eng
  • test.com/eng/page
  • test.com/eng/admin/page

But these links do not work

  • test.com
  • test.com/page
  • test.com/admin/page
$patterns = array(
    ':lang' => 'ua|ru|eng|de'
);
$router->pattern($patterns);

$router->group(':lang?', function($router) {
    $router->get('/', function() {
        return 'Front';
    });

    $router->get('page', function() {
        return 'Front - page';
    });


    $router->group('admin', function($router) {
        $router->get('/', function() {
            return 'Admin';
        });

        $router->get('page', function() {
            return 'Admin - page';
        });
    });
});

How to solve this problem? Thank you in advance.

@SlamDevelop
Copy link
Author

@izniburak Can you help me figure it out?

@izniburak
Copy link
Owner

Hi @SlamDevelop ,
I will check the problem in my local and give information about that to you. If there is any problem about patterns for Groups, I will try to fix it and release a new version ASAP.

Thanks.

@izniburak izniburak added the bug label Dec 3, 2021
@Evrim267
Copy link

Hi @izniburak and @SlamDevelop did you find a solution for this problem?

@AlexJMcloud
Copy link

Most likely I understand how to solve it. You create your own pattern, but you do not add it to the existing class pattern. Here you can solve the methods of adding to the array of patterns.

@AlexJMcloud
Copy link

/** * @param array $pattern * @return bool */ protected function addPattern(array $pattern):bool { if(!empty($pattern)) { $this->patterns = array_merge($this->patterns, $pattern); return true; } return false; }

@AlexJMcloud
Copy link

$patterns = array( ':lang' => 'ua|ru|eng|de' ); $router->addPattern($patterns);

@AlexJMcloud
Copy link

"$router->pattern($patterns);" - Haven't tried using the built-in . But I'll watch it later

@AlexJMcloud
Copy link

`/**
* Add new route rules pattern; String or Array
*
* @param array|string $pattern
* @param string|null $attr
*
* @return true
* @throws
*/
public function pattern(array|string $pattern, string $attr = null): bool
{
if (is_array($pattern)) {
foreach ($pattern as $key => $value) {
if (array_key_exists($key, $this->patterns)) {
$this->exception($key . ' pattern cannot be changed.');
}
$this->patterns[$key] = '(' . $value . ')';
}
} else {
if (array_key_exists($pattern, $this->patterns)) {
$this->exception($pattern . ' pattern cannot be changed.');
}
$this->patterns[$pattern] = '(' . $attr . ')';
}

    return true;
}`

Or replace with this. My language change works within the pattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants