FriendsOfSymfony / FOSJsRoutingBundle

A pretty nice way to expose your Symfony routing to client applications.
1.48k stars 261 forks source link

Inform about JMSI18nRoutingBundle compatibility #352

Closed benjamintoussaint closed 5 years ago

benjamintoussaint commented 5 years ago

I've spent the last day trying to figure out why routes could not be dumped anymore to JS in my app. I'm hoping to save hours of trouble for other developers who would encounter the exact same issue.

exposed domain feature introduced in FOSJsRoutingBundle 2.3.0 unfortunately prevents dumping JMSI18nRoutingBundle internationalized routes.

The exposed routes defined in app/config/config.yml are now transformed into a regular expression:

(?P<default>route_1|route_2)

The problem when using JMSI18nRoutingBundle is that the names of the routes are locale-prefixed, so that in the loop through all routes:

foreach ($collection->all() as $name => $route) {

The variable $name would not be route_1 or route_2, such as defined in app/config/config.yml, but rather en__RG__route_1 or en__RG__route_2.

So when populating the $matches array using the original routes names and then attempting to get their corresponding domain using the locale-prefixed routes names, well, that returns null and continues the loop:

preg_match('#' . $this->pattern . '#', $name, $matches);

if (count($matches) === 0) {
    continue;
}

$domain = $this->getDomainByRouteMatches($matches, $name);

if (is_null($domain)) {
    continue;
}

After some thoughts, I've stumbled upon a very easy solution consisting in adapting the routes defined in app/config/config.yml so that they would be used as locale-prefixed in the loop!

Tada :)

I hope that it's the right place to inform about this issue, please let me know otherwise, thank you.

benjamintoussaint commented 5 years ago

Please let me know if you need more information 🤓

tobias-93 commented 5 years ago

Thanks @benjamintoussaint, this combines great with #334