|
20 | 20 | - [Generate localized route URL's](#-generate-route-urls) using the `route()` helper. |
21 | 21 | - [Redirect to localized routes](#-redirect-to-routes) using the `redirect()->route()` helper. |
22 | 22 | - [Generate localized signed route URL's](#-generate-signed-route-urls). |
| 23 | +- [Generate localized versions of the current URL](#-generate-localized-versions-of-the-current-url) with our `Route::localizedUrl()` macro. |
23 | 24 | - [Automatically set the appropriate app locale](#%EF%B8%8F-use-middleware-to-update-app-locale) using the middleware. |
24 | 25 | - Optionally [detect and set the preferred locale in multiple sources](#%EF%B8%8F-use-localizer-to-detect-and-set-the-locale). |
25 | 26 | - Use localized route keys with [route model binding](#-route-model-binding). |
@@ -327,6 +328,62 @@ You can't redirect to URL's in a specific locale this way, but if you need to, y |
327 | 328 | return redirect(route('about', [], true, 'nl')); // localized route, redirects to /nl/about |
328 | 329 | ``` |
329 | 330 |
|
| 331 | +### ⚓️ Generate Localized Versions of the Current URL |
| 332 | + |
| 333 | +To generate a URL for the current route in any locale, you can use this `Route` macro: |
| 334 | + |
| 335 | +##### With Route Model Binding |
| 336 | + |
| 337 | +If your route uses a localized key (like a slug) and you are using [route model binding](#-route-model-binding), |
| 338 | +then the key will automatically be localized. |
| 339 | + |
| 340 | +```php |
| 341 | +$current = \Route::localizedUrl(); // /en/posts/en-slug |
| 342 | +$en = \Route::localizedUrl('en'); // /en/posts/en-slug |
| 343 | +$nl = \Route::localizedUrl('nl'); // /nl/posts/nl-slug |
| 344 | +``` |
| 345 | + |
| 346 | +If you have a route **with multiple keys**, like `/en/posts/{id}/{slug}`, then you can pass the parameters yourself |
| 347 | +(like in the example without route model binding below) or you can implement this interface in your model: |
| 348 | + |
| 349 | +```php |
| 350 | +use CodeZero\LocalizedRoutes\ProvidesRouteParameters; |
| 351 | +use Illuminate\Database\Eloquent\Model; |
| 352 | + |
| 353 | +class Post extends Model implements ProvidesRouteParameters |
| 354 | +{ |
| 355 | + public function getRouteParameters($locale = null) |
| 356 | + { |
| 357 | + return [ |
| 358 | + $this->id, |
| 359 | + $this->getSlug($locale) // Add this method yourself of course :) |
| 360 | + ]; |
| 361 | + } |
| 362 | +} |
| 363 | +``` |
| 364 | + |
| 365 | +Now, as long as you use route model binding, you can still just do: |
| 366 | + |
| 367 | +```php |
| 368 | +$current = \Route::localizedUrl(); // /en/posts/en-slug |
| 369 | +$en = \Route::localizedUrl('en'); // /en/posts/en-slug |
| 370 | +$nl = \Route::localizedUrl('nl'); // /nl/posts/nl-slug |
| 371 | +``` |
| 372 | + |
| 373 | +##### Without Route Model Binding |
| 374 | + |
| 375 | +If you don't use [route model binding](#-route-model-binding) and you need a localized slug in the URL, |
| 376 | +then you will have to pass it manually. |
| 377 | + |
| 378 | +For example: |
| 379 | + |
| 380 | +```php |
| 381 | +$nl = \Route::localizedUrl('nl'); // Wrong: /nl/posts/en-slug |
| 382 | +$nl = \Route::localizedUrl('nl', [$post->getSlug('nl')]); // Right: /nl/posts/nl-slug |
| 383 | +``` |
| 384 | + |
| 385 | +The `getSlug()` method is just for illustration, so you will need to implement that yourself of course. |
| 386 | + |
330 | 387 | ### ✍🏻 Generate Signed Route URL's |
331 | 388 |
|
332 | 389 | Generating a [signed route URL](https://laravel.com/docs/5.8/urls#signed-urls) is just as easy. |
|
0 commit comments