Skip to content

Commit dc0b3f8

Browse files
committed
Update README
1 parent 42ed06f commit dc0b3f8

File tree

1 file changed

+77
-31
lines changed

1 file changed

+77
-31
lines changed

README.md

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
#### A convenient way to set up, manage and use localized routes in a Laravel app.
1313

1414
- [Automatically register](#-register-routes) a route for each locale you wish to support.
15-
- Use [route slugs or custom domains](#-supported-locales) (or subdomains)
16-
- Optionally remove the locale slug from the URL for your main language.
17-
- [Generate localized route URL's](#-generate-route-urls) in the simplest way using the `route()` helper.
15+
- Use [URL slugs or custom domains](#-supported-locales) (or subdomains).
16+
- Optionally omit the locale slug from the URL for your main locale.
17+
- [Generate localized route URL's](#-generate-route-urls) using the `route()` helper.
1818
- [Redirect to localized routes](#-redirect-to-routes) using the `redirect()->route()` helper.
1919
- [Generate localized signed route URL's](#-generate-signed-route-urls)
2020
- Allow routes to be [cached](#-cache-routes).
2121
- Optionally [translate each segment](#-translate-routes) in your URI's.
22-
- **Let you work with routes without thinking too much about locales.**
22+
- Use the middleware to [automatically set the appropriate app locale](#-use-middleware-to-update-app-locale) (and use route model binding).
23+
- **Work with routes without thinking too much about locales.**
2324

2425
## ✅ Requirements
2526

@@ -28,7 +29,7 @@
2829

2930
## 📦 Install
3031

31-
```php
32+
```bash
3233
composer require codezero/laravel-localized-routes
3334
```
3435

@@ -84,15 +85,53 @@ Setting this option to `'en'` will result, for example, in URL's like this:
8485

8586
> This option has no effect if you use domains instead of slugs.
8687
87-
#### Automatically Set Locale for Localized Routes
88+
#### ☑️ Use Middleware to Update App Locale
89+
90+
By default, the app locale will always be what you configured in `config/app.php`. To automatically update the app locale when a localized route is accessed, you need to use a middleware.
91+
92+
**⚠️ Important note for Laravel 6+**
93+
94+
To make route model binding work in Laravel 6+ you always also need to add the middleware to the `$middlewarePriority` array in `app/Http/Kernel.php` so it runs before `SubstituteBindings`:
95+
96+
```php
97+
protected $middlewarePriority = [
98+
\Illuminate\Session\Middleware\StartSession::class, // <= after this
99+
//...
100+
\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class,
101+
//...
102+
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
103+
];
104+
```
105+
106+
You can then enable the middleware in a few ways:
88107

89-
To automatically set the locale when a localized route is active via a middleware simply set the option to true:
108+
**For every route, via our config file**
109+
110+
Simply set the option to `true`:
90111

91112
```php
92113
'use_locale_middleware' => true
93114
```
94115

95-
Alternatively, you can omit it completely or specify it for a specific route or route group:
116+
**OR, for every route using the `web` middleware group**
117+
118+
You can manually add the middleware to the `$middlewareGroups` array in `app/Http/Kernel.php`:
119+
120+
```php
121+
protected $middlewareGroups = [
122+
'web' => [
123+
\Illuminate\Session\Middleware\StartSession::class, // <= after this
124+
//...
125+
\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class,
126+
//...
127+
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
128+
],
129+
];
130+
```
131+
132+
**OR, for specific routes**
133+
134+
Alternatively, you can add the middleware to a specific route or route group:
96135

97136
```php
98137
Route::localized(function () {
@@ -101,22 +140,22 @@ Route::localized(function () {
101140
->name('about')
102141
->middleware(\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class);
103142

104-
Route::group(
105-
[
106-
'as' => 'admin.',
107-
'middleware' => [\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class],
108-
],
109-
function () {
110-
Route::get('admin/reports', ReportsController::class.'@index')
111-
->name('reports.index');
112-
});
143+
Route::group([
144+
'as' => 'admin.',
145+
'middleware' => [\CodeZero\LocalizedRoutes\Middleware\LocalizedRouteLocaleHandler::class],
146+
], function () {
147+
148+
Route::get('admin/reports', ReportsController::class.'@index')
149+
->name('reports.index');
150+
151+
});
113152

114153
});
115154
```
116155

117156
#### ☑️ Set Options for the Current Localized Route Group
118157

119-
To set an option for one localized route group only, you can specify it as the second parameter of the localized route macro:
158+
To set an option for one localized route group only, you can specify it as the second parameter of the localized route macro. This will override the config file settings.
120159

121160
```php
122161
Route::localized(function () {
@@ -192,11 +231,18 @@ $url = route(app()->getLocale().'.admin.reports.index');
192231

193232
Because the former is rather ugly, this package overwrites the `route()` function and the underlying `UrlGenerator` class with an additional, optional `$locale` argument and takes care of the locale prefix for you. If you don't specify a locale, either a normal, non-localized route or a route in the current locale is returned.
194233

234+
```php
235+
$url = route('admin.reports.index'); // current locale
236+
$url = route('admin.reports.index', [], true, 'nl'); // dutch URL
237+
```
238+
239+
This is the new route helper signature:
240+
195241
```php
196242
route($name, $parameters = [], $absolute = true, $locale = null)
197243
```
198244

199-
A few examples:
245+
A few examples (given the example routes we registered above):
200246

201247
```php
202248
app()->setLocale('en');
@@ -222,14 +268,14 @@ $url = route('en.about', [], true, 'nl'); // /nl/about
222268
Laravel's `Redirector` uses the same `UrlGenerator` as the `route()` function behind the scenes. Because we are overriding this class, you can easily redirect to your routes.
223269

224270
```php
225-
return redirect()->route('home'); // redirects to /home
226-
return redirect()->route('about'); // redirects to /en/about (current locale)
271+
return redirect()->route('home'); // non-localized route, redirects to /home
272+
return redirect()->route('about'); // localized route, redirects to /en/about (current locale)
227273
```
228274

229275
You can't redirect to URL's in a specific locale this way, but if you need to, you can of course just use the `route()` function.
230276

231277
```php
232-
return redirect(route('about', [], true, 'nl')); // redirects to /nl/about
278+
return redirect(route('about', [], true, 'nl')); // localized route, redirects to /nl/about
233279
```
234280

235281
### ✍🏻 Generate Signed Route URL's
@@ -291,15 +337,15 @@ The above will generate:
291337

292338
> If a translation is not found, the original segment is used.
293339
294-
## 🚏 Route Placeholders
340+
## 🚏 Route Parameters
295341

296-
Placeholders are not translated via language files. These are values you would provide via the `route()` function. The `Lang::uri()` macro will skip any placeholder segment.
342+
Parameter placeholders are not translated via language files. These are values you would provide via the `route()` function. The `Lang::uri()` macro will skip any parameter placeholder segment.
297343

298344
If you have a model that uses a route key that is translated in the current locale, then you can still simply pass the model to the `route()` function to get translated URL's.
299345

300346
An example...
301347

302-
#### Given we have a model like this:
348+
**Given we have a model like this:**
303349

304350
```php
305351
class Post extends \Illuminate\Database\Eloquent\Model
@@ -318,7 +364,7 @@ class Post extends \Illuminate\Database\Eloquent\Model
318364

319365
> **TIP:** checkout [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) for translatable models.
320366
321-
#### If we have a localized route like this:
367+
**If we have a localized route like this:**
322368

323369
```php
324370
Route::localized(function () {
@@ -329,29 +375,29 @@ Route::localized(function () {
329375
});
330376
```
331377

332-
#### We can now get the URL with the appropriate slug:
378+
**We can now get the URL with the appropriate slug:**
333379

334380
```php
335381
app()->setLocale('en');
336382
app()->getLocale(); // 'en'
337383

338384
$post = new Post;
339385

340-
$url = route('posts.show', $post); // /en/posts/en-slug
341-
$url = route('posts.show', $post, true, 'nl'); // /nl/posts/nl-slug
386+
$url = route('posts.show', [$post]); // /en/posts/en-slug
387+
$url = route('posts.show', [$post], true, 'nl'); // /nl/posts/nl-slug
342388
```
343389

344390
## 🗃 Cache Routes
345391

346392
In production you can safely cache your routes per usual.
347393

348-
```php
394+
```bash
349395
php artisan route:cache
350396
```
351397

352398
## 🚧 Testing
353399

354-
```
400+
```bash
355401
composer test
356402
```
357403

0 commit comments

Comments
 (0)