Skip to content

Commit 3764780

Browse files
woodspireMarc Cámara
authored and
Marc Cámara
committed
Fix replacing route parameter with correct key with implicit paramet… (#552)
* Fix replacing route parameter with correct key with implicite parameter bindings. Inspired by Illuminate\Routing\UrlGenerator::formatParameters() * Update readme
1 parent 6cd1756 commit 3764780

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Easy i18n localization for Laravel, an useful tool to combine with Laravel local
1616
- <a href="#usage">Usage</a>
1717
- <a href="#middleware">Middleware</a>
1818
- <a href="#helpers">Helpers</a>
19+
- <a href="#route-model-binding">Route Model Binding</a>
1920
- <a href="#translated-routes">Translated Routes</a>
2021
- <a href="#config">Config</a>
2122
- <a href="#config-files">Config files</a>
@@ -194,6 +195,11 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = array
194195

195196
It returns a URL localized to the desired locale.
196197

198+
##### Route Model Binding
199+
200+
Note that [route model binding]([https://laravel.com/docs/master/routing#route-model-binding]) is taken into account when generating the localized route.
201+
202+
197203
### Get Clean routes
198204

199205
```php
@@ -386,6 +392,8 @@ If you're supporting multiple locales in your project you will probably want to
386392
```
387393
Here default language will be forced in getLocalizedURL() to be present in the URL even `hideDefaultLocaleInURL = true`.
388394

395+
Note that <a href="#route-model-binding">Route Model Binding</a> is supported.
396+
389397
## Translated Routes
390398

391399
You can adapt your URLs depending on the language you want to show them. For example, http://url/en/about and http://url/es/acerca (acerca is about in spanish) or http://url/en/view/5 and http://url/es/ver/5 (view == ver in spanish) would be redirected to the same controller using the proper filter and setting up the translation files as follows:

src/Mcamara/LaravelLocalization/LaravelLocalization.php

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mcamara\LaravelLocalization;
44

55
use Illuminate\Config\Repository;
6+
use Illuminate\Contracts\Routing\UrlRoutable;
67
use Illuminate\Foundation\Application;
78
use Illuminate\Http\Request;
89
use Illuminate\Routing\Router;
@@ -551,6 +552,9 @@ public function checkLocaleInSupportedLocales($locale)
551552
protected function substituteAttributesInRoute($attributes, $route)
552553
{
553554
foreach ($attributes as $key => $value) {
555+
if ($value instanceOf UrlRoutable) {
556+
$value = $value->getRouteKey();
557+
}
554558
$route = str_replace('{'.$key.'}', $value, $route);
555559
$route = str_replace('{'.$key.'?}', $value, $route);
556560
}

0 commit comments

Comments
 (0)