@@ -153,30 +153,43 @@ Route::localized(function () {
153
153
## 🧩 Add Middleware to Update App Locale
154
154
155
155
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 ` .
157
158
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 ` .
159
165
160
166
``` 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
+ })
167
177
```
168
178
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:
171
180
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 ` .
173
182
174
183
``` 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
+ ],
180
193
];
181
194
```
182
195
0 commit comments