Skip to content

Commit 7324f4a

Browse files
authored
Simplify middleware priority (#18)
1 parent 853ce4a commit 7324f4a

File tree

4 files changed

+68
-34
lines changed

4 files changed

+68
-34
lines changed

README.md

+7-16
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,23 @@ Laravel will automatically register the ServiceProvider.
4040
## 🧩 Add Middleware
4141

4242
Add the middleware to the `web` middleware group in `app/Http/Kernel.php`.
43-
Make sure to add it after `StartSession` and before `SubstituteBindings`:
43+
Make sure to add it after `StartSession` and before `SubstituteBindings`.
44+
45+
The order of the middleware is important if you are using localized route keys (translated slugs)!
46+
The session needs to be active when setting the locale, and the locale needs to be set when substituting the route bindings.
4447

4548
```php
4649
protected $middlewareGroups = [
4750
'web' => [
51+
//...
52+
\Illuminate\Session\Middleware\StartSession::class, // <= after this
4853
//...
4954
\CodeZero\Localizer\Middleware\SetLocale::class,
55+
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
5056
],
5157
];
5258
```
5359

54-
You also need to add the middleware to the `$middlewarePriority` array in `app/Http/Kernel.php`
55-
to trigger it in the correct order:
56-
57-
```php
58-
protected $middlewarePriority = [
59-
\Illuminate\Session\Middleware\StartSession::class, // <= after this
60-
//...
61-
\CodeZero\Localizer\Middleware\SetLocale::class,
62-
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
63-
];
64-
```
65-
66-
If you don't see the `$middlewarePriority` array in your kernel file,
67-
then you can copy it over from the parent class `Illuminate\Foundation\Http\Kernel`.
68-
6960
## ⚙ Configure
7061

7162
### Publish Configuration File

tests/Feature/SetLocaleTest.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function it_looks_for_a_locale_in_a_custom_route_action()
4444
Route::group($routeAction, function () {
4545
Route::get('some/route', function () {
4646
return App::getLocale();
47-
})->middleware(['web', SetLocale::class]);
47+
})->middleware(['web']);
4848
});
4949

5050
$response = $this->get('some/route');
@@ -62,7 +62,7 @@ public function it_looks_for_a_locale_in_the_url()
6262

6363
Route::get('nl/some/route', function () {
6464
return App::getLocale();
65-
})->middleware(['web', SetLocale::class]);
65+
})->middleware(['web']);
6666

6767
$response = $this->get('nl/some/route');
6868

@@ -81,7 +81,7 @@ public function you_can_configure_which_segment_to_use_as_locale()
8181

8282
Route::get('some/nl/route', function () {
8383
return App::getLocale();
84-
})->middleware(['web', SetLocale::class]);
84+
})->middleware(['web']);
8585

8686
$response = $this->get('some/nl/route');
8787

@@ -101,7 +101,7 @@ public function it_looks_for_custom_slugs()
101101

102102
Route::get('dutch/some/route', function () {
103103
return App::getLocale();
104-
})->middleware(['web', SetLocale::class]);
104+
})->middleware(['web']);
105105

106106
$response = $this->get('dutch/some/route');
107107

@@ -121,11 +121,11 @@ public function you_can_use_multiple_slugs_for_a_locale()
121121

122122
Route::get('dutch/some/route', function () {
123123
return App::getLocale();
124-
})->middleware(['web', SetLocale::class]);
124+
})->middleware(['web']);
125125

126126
Route::get('nederlands/some/route', function () {
127127
return App::getLocale();
128-
})->middleware(['web', SetLocale::class]);
128+
})->middleware(['web']);
129129

130130
$response = $this->get('dutch/some/route');
131131

@@ -152,7 +152,7 @@ public function it_looks_for_custom_domains()
152152
Route::group(['domain' => 'dutch.test'], function () {
153153
Route::get('some/route', function () {
154154
return App::getLocale();
155-
})->middleware(['web', SetLocale::class]);
155+
})->middleware(['web']);
156156
});
157157

158158
$response = $this->get('http://dutch.test/some/route');
@@ -174,13 +174,13 @@ public function you_can_use_multiple_domains_for_a_locale()
174174
Route::group(['domain' => 'dutch.test'], function () {
175175
Route::get('some/route', function () {
176176
return App::getLocale();
177-
})->middleware(['web', SetLocale::class]);
177+
})->middleware(['web']);
178178
});
179179

180180
Route::group(['domain' => 'nederlands.test'], function () {
181181
Route::get('some/route', function () {
182182
return App::getLocale();
183-
})->middleware(['web', SetLocale::class]);
183+
})->middleware(['web']);
184184
});
185185

186186
$response = $this->get('http://dutch.test/some/route');
@@ -206,7 +206,7 @@ public function it_checks_for_a_configured_omitted_locale()
206206

207207
Route::get('some/route', function () {
208208
return App::getLocale();
209-
})->middleware(['web', SetLocale::class]);
209+
})->middleware(['web']);
210210

211211
$response = $this->get('some/route');
212212

@@ -227,7 +227,7 @@ public function it_looks_for_a_locale_on_the_authenticated_user()
227227

228228
Route::get('some/route', function () {
229229
return App::getLocale();
230-
})->middleware(['web', SetLocale::class]);
230+
})->middleware(['web']);
231231

232232
$response = $this->actingAs($user)->get('some/route');
233233

@@ -252,7 +252,7 @@ public function it_will_bypass_missing_attribute_exception_if_the_locale_attribu
252252

253253
Route::get('some/route', function () {
254254
return App::getLocale();
255-
})->middleware(['web', SetLocale::class]);
255+
})->middleware(['web']);
256256

257257
$response = $this->actingAs($user)->get('some/route');
258258

@@ -271,7 +271,7 @@ public function it_looks_for_a_locale_in_the_session()
271271

272272
Route::get('some/route', function () {
273273
return App::getLocale();
274-
})->middleware(['web', SetLocale::class]);
274+
})->middleware(['web']);
275275

276276
$response = $this->get('some/route');
277277

@@ -290,7 +290,7 @@ public function it_looks_for_a_locale_in_a_cookie()
290290

291291
Route::get('some/route', function () {
292292
return App::getLocale();
293-
})->middleware(['web', SetLocale::class]);
293+
})->middleware(['web']);
294294

295295
$response = $this->withCookie($this->cookieName, $cookie)
296296
->get('some/route');
@@ -310,7 +310,7 @@ public function it_looks_for_a_locale_in_the_browser()
310310

311311
Route::get('some/route', function () {
312312
return App::getLocale();
313-
})->middleware(['web', SetLocale::class]);
313+
})->middleware(['web']);
314314

315315
$response = $this->get('some/route');
316316

@@ -329,7 +329,7 @@ public function it_returns_the_best_match_when_a_browser_locale_is_used()
329329

330330
Route::get('some/route', function () {
331331
return App::getLocale();
332-
})->middleware(['web', SetLocale::class]);
332+
})->middleware(['web']);
333333

334334
$response = $this->get('some/route');
335335

@@ -346,7 +346,7 @@ public function it_looks_for_the_current_app_locale()
346346

347347
Route::get('some/route', function () {
348348
return App::getLocale();
349-
})->middleware(['web', SetLocale::class]);
349+
})->middleware(['web']);
350350

351351
$response = $this->get('some/route');
352352

@@ -370,7 +370,7 @@ public function trusted_detectors_ignore_supported_locales_and_may_set_any_local
370370
Route::group($routeAction, function () {
371371
Route::get('some/route', function () {
372372
return App::getLocale();
373-
})->middleware(['web', SetLocale::class]);
373+
})->middleware(['web']);
374374
});
375375

376376
$response = $this->get('some/route');

tests/Stubs/Kernel.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace CodeZero\Localizer\Tests\Stubs;
4+
5+
use Illuminate\Foundation\Http\Kernel as HttpKernel;
6+
7+
class Kernel extends HttpKernel
8+
{
9+
/**
10+
* The application's route middleware groups.
11+
*
12+
* @var array
13+
*/
14+
protected $middlewareGroups = [
15+
'web' => [
16+
\Illuminate\Cookie\Middleware\EncryptCookies::class,
17+
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
18+
\Illuminate\Session\Middleware\StartSession::class,
19+
// \Illuminate\Session\Middleware\AuthenticateSession::class,
20+
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
21+
\CodeZero\Localizer\Middleware\SetLocale::class, // <== Added Middleware Here
22+
\Illuminate\Routing\Middleware\SubstituteBindings::class,
23+
],
24+
];
25+
}

tests/TestCase.php

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ protected function setUp(): void
2121
Config::set('app.key', Str::random(32));
2222
}
2323

24+
/**
25+
* Resolve application Console Kernel implementation.
26+
*
27+
* @param \Illuminate\Foundation\Application $app
28+
*
29+
* @return void
30+
*/
31+
protected function resolveApplicationHttpKernel($app): void
32+
{
33+
// In Laravel 6+, we need to add the middleware to
34+
// $middlewarePriority in Kernel.php for route
35+
// model binding to work properly.
36+
$app->singleton(
37+
'Illuminate\Contracts\Http\Kernel',
38+
'CodeZero\Localizer\Tests\Stubs\Kernel'
39+
);
40+
}
41+
2442
/**
2543
* Get the packages service providers.
2644
*

0 commit comments

Comments
 (0)