xfra35 / f3-multilang

Create multilingual apps with this localization plugin for the PHP Fat-Free Framework
GNU General Public License v3.0
48 stars 13 forks source link

Reroute bug? #4

Closed rafatrace closed 9 years ago

rafatrace commented 9 years ago

Hello mate, does rerouting function from f3 conflicts with multilang?

I got this line of code:

$f3->reroute('/'.$route);

This reroutes to app-path.com/route insted of app-path.com/en/route.

Should I use?

$f3->reroute('/'.$m->current().'/'.$route);

It was nice if this was automatically ;)

Thanks for your time, cheers.

xfra35 commented 9 years ago

Hi @rafamds, I don't think we can call it a conflict. From the framework's point of view, ROUTES can contain any kind of routes (with or without language prefix), so an automatic prefix would not fit all situations.

Also note that the core reroute function works well with named routes. See :

$f3->reroute('@contact');//reroute to the current language "contact" route (i.e prefixed)
rafatrace commented 9 years ago

I understand, thanks for the exaplanation. Cheers

xfra35 commented 9 years ago

Hi @rafamds,

After giving it more thought, I came to the conclusion that the plugin should provide a mean to quickly localize existing monolingual projects, even those with unnamed routes.

For that purpose, I've added a reroute method to the plugin, which automatically prefixes the provided URI, if needed:

$ml->reroute('/en/contact'); // OK
$ml->reroute('/contact'); // OK => reroute to /xx/contact where xx is the current language

If you don't want to rewrite all your $f3->reroute into $ml->reroute, you can simply use the framework's ONREROUTE hook:

$f3->set('ONREROUTE',function($url,$permanent) use($f3,$ml){
  $f3->clear('ONREROUTE');// avoid infinite loop
  $ml->reroute($url,$permanent);
});

After which $f3->reroute('/contact') will reroute to /xx/contact/.

NB: I've updated the docs.

rafatrace commented 9 years ago

Wow, nice work @xfra35, this was exactly what I was looking for. I prefer to use $ml->reroute() and rewrite everything, and I advice everyone to do this, because in this way you can spot a diference between $f3 and $ml rerouting, and you or other developer that comes to help know that theres a difference.

Excellent work mate, glad to know you're improving this. Cheers.