|
19 | 19 | - [Generate localized signed route URL's](#-generate-signed-route-urls) |
20 | 20 | - Allow routes to be [cached](#-cache-routes). |
21 | 21 | - Optionally [translate each segment](#-translate-routes) in your URI's. |
22 | | -- Use the middleware to [automatically set the appropriate app locale](#-use-middleware-to-update-app-locale) (and use route model binding). |
| 22 | +- Use the middleware to [automatically set the appropriate app locale](#-use-middleware-to-update-app-locale) (and use [route model binding](#-route-model-binding)). |
23 | 23 | - **Work with routes without thinking too much about locales.** |
24 | 24 |
|
25 | 25 | ## 🔌 Demo App |
@@ -394,6 +394,26 @@ $url = route('posts.show', [$post]); // /en/posts/en-slug |
394 | 394 | $url = route('posts.show', [$post], true, 'nl'); // /nl/posts/nl-slug |
395 | 395 | ``` |
396 | 396 |
|
| 397 | +## 🚴 Route Model Binding |
| 398 | + |
| 399 | +If you enable the [middleware](#-use-middleware-to-update-app-locale) included in this package, |
| 400 | +you can use [Laravel's route model binding](https://laravel.com/docs/routing#route-model-binding) |
| 401 | +to automatically inject models with localized route keys in your controllers. |
| 402 | + |
| 403 | +All you need to do is add a `resolveRouteBinding()` method to your model. |
| 404 | +Check [Laravel's documentation](https://laravel.com/docs/routing#route-model-binding) |
| 405 | +for alternative ways to enable route model binding. |
| 406 | + |
| 407 | +```php |
| 408 | +public function resolveRouteBinding($value) |
| 409 | +{ |
| 410 | + // Perform the logic to find the given slug in the database... |
| 411 | + return $this->where('slug->'.app()->getLocale(), $value)->firstOrFail(); |
| 412 | +} |
| 413 | +``` |
| 414 | + |
| 415 | +> **TIP:** checkout [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) for translatable models. |
| 416 | +
|
397 | 417 | ## 🗃 Cache Routes |
398 | 418 |
|
399 | 419 | In production you can safely cache your routes per usual. |
|
0 commit comments