Skip to content

Commit

Permalink
NEW: language-aware reroute function (for unnamed routes essentially)
Browse files Browse the repository at this point in the history
  • Loading branch information
xfra35 committed Mar 5, 2015
1 parent c6f1e3e commit 55f9201
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Demo: [here](https://ml.aesx.fr).
* [Rewrite URLs](#rewrite-urls)
* [Exclude a language from a route](#exclude-a-language-from-a-route)
* [Global routes](#global-routes)
* [Note on rerouting](#note-on-rerouting)
* [API](#api)
* [Potential improvements](#potential-improvements)

Expand Down Expand Up @@ -124,6 +125,47 @@ global = alias1, alias2, alias3
**NB:** on a global route, the language is auto-detected by default.
So in the case of a monolingual back office, you may need to force the language at the controller level.

## Note on rerouting

### If you're using named routes...

`$f3->reroute` will work as expected, that is to say, it will reroute to the
current language URL of the provided named route. E.g:

```php
$f3->reroute('@contact'); // OK => reroute to /xx/contact where xx is the current language
```

### If you're using unnamed routes...

In that case, you have to provide a language-prefixed URL to `$f3->reroute`:

```php
$f3->reroute('/en/contact'); // OK
$f3->reroute('/contact'); // Error => 404 Not Found
```

If you'd prefer to give the short URL to the framework and have it automatically prefix the URL with the current language,
use the `$ml->reroute` method provided by the plugin:

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

In the situation where you'd like to quickly localize an existing project with unnamed routes, and would prefer to avoid
having to rewrite every `$f3->reroute` into `$ml->reroute`, you can simply use the framework's `ONREROUTE` hook:

```php
$f3->set('ONREROUTE',function($url,$permanent) use($f3,$ml){
$f3->clear('ONREROUTE');
$ml->reroute($url,$permanent);
});

// then in your controller, existing reroutes will keep on working:
$f3->reroute('/contact'); // OK => reroute to /xx/contact where xx is the current language
```

## API

```php
Expand Down Expand Up @@ -199,13 +241,26 @@ $ml->isGlobal('captcha');// TRUE

**Assemble url from alias name**

This function is a language-aware version of the `$f3->alias()` function.
This function is a language-aware version of `$f3->alias()`.

```php
echo $ml->alias('terms',NULL,'es');// /es/terminos-y-condiciones [local route]
echo $ml->alias('captcha');// /captcha [global route]
```

### reroute( $url=NULL, $permanent=FALSE )

**Reroute to specified URI**

This function is a language-aware version of `$f3->reroute()`.

Use it if you want an automatic language prefix on **unnamed** routes. Cf. this [note](#note-on-rerouting).

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

## Potential improvements

* Allow domain level recognition (mydomain.jp/es, or jp/es.mydomain.com)
Expand Down
14 changes: 13 additions & 1 deletion lib/multilang.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ function languages() {
return array_keys($this->languages);
}

/**
* Language-aware reroute (autoprefix unnamed routes)
* @param string $url
* @param bool $permanent
* @return NULL
*/
function reroute($url=NULL,$permanent=FALSE) {
if (preg_match('/^\/([^\/]*)/',$url,$m) && !array_key_exists($m[1],$this->languages))
$url=rtrim('/'.$this->current.$url,'/');
$this->f3->reroute($url,$permanent);
}

//! Detects the current language
protected function detect($uri=NULL) {
$this->current=$this->primary;
Expand Down Expand Up @@ -189,4 +201,4 @@ function __construct() {
$this->f3->route('GET /',@$config['root']?:function($f3) use($self){$f3->reroute('/'.$self->current);});
}

}
}
32 changes: 32 additions & 0 deletions tests/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,38 @@ function run($f3) {
$ml->alias('resize','format=big,file=foo.gif','it')==='/resize/big/foo.gif',
'Alias function (global route)'
);
//rerouting
$f3->set('ONREROUTE',function($url,$permanent) use($f3){
$f3->set('rerouted',$url);
});
$f3->clear('rerouted');
$ml->reroute('@blogEntry(slug=hallo-welt)');
$test->expect(
$f3->get('rerouted')=='/de/blog/hallo-welt',
'Reroute to a named rewritten route'
);
$f3->clear('rerouted');
$ml->reroute('@resize(format=big,file=foo.gif)');
$test->expect(
$f3->get('rerouted')=='/resize/big/foo.gif',
'Reroute to a named global route'
);
$ok=TRUE;
$reroutes=array(
NULL=>$f3->REALM,
'/'=>'/de',
'/blog/hallo-welt'=>'/de/blog/hallo-welt',
'/de/blog/hallo-welt'=>'/de/blog/hallo-welt',
);
foreach($reroutes as $url=>$expected) {
$f3->clear('rerouted');
$ml->reroute($url);
$ok=$ok && $f3->get('rerouted')==$expected;
}
$test->expect(
$ok,
'Reroute to any unnamed route (auto prefix)'
);
//helper functions
$test->expect(
$ml->isGlobal('resize') && !$ml->isGlobal('blogEntry'),
Expand Down

0 comments on commit 55f9201

Please sign in to comment.