Skip to content

Commit 954033b

Browse files
committed
Add Laravel 11 middleware instructions to README
1 parent 6d22285 commit 954033b

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

README.md

+29-16
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,43 @@ Route::localized(function () {
153153
## 🧩 Add Middleware to Update App Locale
154154

155155
By default, the app locale will always be what you configured in `config/app.php`.
156-
To automatically update the app locale, you need to register the middleware.
156+
To automatically update the app locale, you need to register the middleware in the `web` middleware group.
157+
Make sure to add it after `StartSession` and before `SubstituteBindings`.
157158

158-
Add the middleware to the `web` middleware group in `app/Http/Kernel.php`:
159+
The order of the middleware is important if you are using localized route keys (translated slugs)!
160+
The session needs to be active when setting the locale, and the locale needs to be set when substituting the route bindings.
161+
162+
### Laravel 11 and newer:
163+
164+
Add the middleware to the `web` middleware group in `bootstrap/app.php`.
159165

160166
```php
161-
protected $middlewareGroups = [
162-
'web' => [
163-
//...
164-
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
165-
],
166-
];
167+
// bootstrap/app.php
168+
->withMiddleware(function (Middleware $middleware) {
169+
$middleware->web(remove: [
170+
\Illuminate\Routing\Middleware\SubstituteBindings::class,
171+
]);
172+
$middleware->web(append: [
173+
\CodeZero\Localizer\Middleware\SetLocale::class,
174+
\Illuminate\Routing\Middleware\SubstituteBindings::class,
175+
]);
176+
})
167177
```
168178

169-
You also need to add the middleware to the `$middlewarePriority` array in `app/Http/Kernel.php`.
170-
If you don't see the `$middlewarePriority` array, you can copy it from the parent class `Illuminate\Foundation\Http\Kernel`.
179+
### Laravel 10:
171180

172-
Make sure to add it after `StartSession` and before `SubstituteBindings` to trigger it in the correct order:
181+
Add the middleware to the `web` middleware group in `app/Http/Kernel.php`.
173182

174183
```php
175-
protected $middlewarePriority = [
176-
\Illuminate\Session\Middleware\StartSession::class, // <= after this
177-
//...
178-
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
179-
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
184+
// app/Http/Kernel.php
185+
protected $middlewareGroups = [
186+
'web' => [
187+
//...
188+
\Illuminate\Session\Middleware\StartSession::class, // <= after this
189+
//...
190+
\CodeZero\Localizer\Middleware\SetLocale::class,
191+
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
192+
],
180193
];
181194
```
182195

0 commit comments

Comments
 (0)