Skip to content

Multiple url for single controller #34

@sandline

Description

@sandline

Hi,

I would like to use the same controller for multiple urls (because I show exactly the same data, but just in different languages) and I've tried this code:

    $r->addRoute('GET', '/{lang:(it|ch)}/',       'Home');
    $r->addRoute('GET', '/{lang:(it|ch)}/blog/',  'Blog');
    $r->addRoute('GET', '/sitemap/',       'Sitemap');

but this does not work. I get this error:

Notice: Undefined offset: 4 in /data/www/www.mysite.lan/lib/FastRoute/Dispatcher/GroupCountBased.php on line 16

Call Stack:
    0.0004     273928   1. {main}() /data/www/www.mysite.lan/index.php:0
    0.0257    1815680   2. FastRoute\Dispatcher\RegexBasedAbstract->dispatch() /data/www/www.mysite.lan/index.php:46
    0.0257    1815832   3. FastRoute\Dispatcher\GroupCountBased->dispatchVariableRoute() /data/www/www.mysite.lan/lib/FastRoute/Dispatcher/RegexBasedAbstract.php:20

Warning: Invalid argument supplied for foreach() in /data/www/www.mysite.lan/lib/FastRoute/Dispatcher/GroupCountBased.php on line 20

Call Stack:
    0.0004     273928   1. {main}() /data/www/www.mysite.lan/index.php:0
    0.0257    1815680   2. FastRoute\Dispatcher\RegexBasedAbstract->dispatch() /data/www/www.mysite.lan/index.php:46
    0.0257    1815832   3. FastRoute\Dispatcher\GroupCountBased->dispatchVariableRoute() /data/www/www.mysite.lan/lib/FastRoute/Dispatcher/RegexBasedAbstract.php:20

and with

$r = $d->dispatch(Core\Site::getRequestMethod(), Core\Site::getRequestURL());

$r[0] is empty, so the application does not know what is the controller to use.

I've tried to dump, in GroupCountBased, the route data inside the foreach:

        foreach ($routeData as $data) {
            var_dump($data);
            if (!preg_match($data['regex'], $uri, $matches)) {
                continue;
            }

just to try to understand the problem, and I've got:

array(2) {
  'regex' =>
  string(38) "~^(?|/((it|ch))/|/((it|ch))/blog/())$~"
  [...]

so I think that the problem is related to the fact that routes is "collected" and the use of | (pipe) between multiple urls broke the original regexs.

I've workarounded it redefining urls:

    $r->addRoute('GET', '/it/',       'HomeIT');
    $r->addRoute('GET', '/ch/',       'HomeCH');
    $r->addRoute('GET', '/it/blog/',  'BlogIT');
    $r->addRoute('GET', '/ch/blog/',  'BlogCH');

but this does not satisfy me. There is a way to achieve the use of variable url parts for the same controller and at the same time to obtain from the dispatcher the first part of url (it or ch) as a variable?

Consider that I can't simply do

    $r->addRoute('GET', '/{lang:.*}/',       'Home');

because this will also route the /sitemap/ url (and also any other url) and I want to avoid to serve everything with the same controller.

Thank you for reading,
Marco

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions