From 1346af159fee8d015972dbb430d26b423809ef91 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Fri, 18 Aug 2023 12:29:07 +0200 Subject: [PATCH 1/5] Test kernel middleware priority --- tests/Feature/SetLocaleTest.php | 36 ++++++++++++++++----------------- tests/TestCase.php | 18 +++++++++++++++++ tests/stubs/Kernel.php | 25 +++++++++++++++++++++++ 3 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 tests/stubs/Kernel.php diff --git a/tests/Feature/SetLocaleTest.php b/tests/Feature/SetLocaleTest.php index d0c882c..8b8a099 100644 --- a/tests/Feature/SetLocaleTest.php +++ b/tests/Feature/SetLocaleTest.php @@ -44,7 +44,7 @@ public function it_looks_for_a_locale_in_a_custom_route_action() Route::group($routeAction, function () { Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); }); $response = $this->get('some/route'); @@ -62,7 +62,7 @@ public function it_looks_for_a_locale_in_the_url() Route::get('nl/some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('nl/some/route'); @@ -81,7 +81,7 @@ public function you_can_configure_which_segment_to_use_as_locale() Route::get('some/nl/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('some/nl/route'); @@ -101,7 +101,7 @@ public function it_looks_for_custom_slugs() Route::get('dutch/some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('dutch/some/route'); @@ -121,11 +121,11 @@ public function you_can_use_multiple_slugs_for_a_locale() Route::get('dutch/some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); Route::get('nederlands/some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('dutch/some/route'); @@ -152,7 +152,7 @@ public function it_looks_for_custom_domains() Route::group(['domain' => 'dutch.test'], function () { Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); }); $response = $this->get('http://dutch.test/some/route'); @@ -174,13 +174,13 @@ public function you_can_use_multiple_domains_for_a_locale() Route::group(['domain' => 'dutch.test'], function () { Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); }); Route::group(['domain' => 'nederlands.test'], function () { Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); }); $response = $this->get('http://dutch.test/some/route'); @@ -206,7 +206,7 @@ public function it_checks_for_a_configured_omitted_locale() Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('some/route'); @@ -227,7 +227,7 @@ public function it_looks_for_a_locale_on_the_authenticated_user() Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->actingAs($user)->get('some/route'); @@ -252,7 +252,7 @@ public function it_will_bypass_missing_attribute_exception_if_the_locale_attribu Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->actingAs($user)->get('some/route'); @@ -271,7 +271,7 @@ public function it_looks_for_a_locale_in_the_session() Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('some/route'); @@ -290,7 +290,7 @@ public function it_looks_for_a_locale_in_a_cookie() Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->withCookie($this->cookieName, $cookie) ->get('some/route'); @@ -310,7 +310,7 @@ public function it_looks_for_a_locale_in_the_browser() Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('some/route'); @@ -329,7 +329,7 @@ public function it_returns_the_best_match_when_a_browser_locale_is_used() Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('some/route'); @@ -346,7 +346,7 @@ public function it_looks_for_the_current_app_locale() Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); $response = $this->get('some/route'); @@ -370,7 +370,7 @@ public function trusted_detectors_ignore_supported_locales_and_may_set_any_local Route::group($routeAction, function () { Route::get('some/route', function () { return App::getLocale(); - })->middleware(['web', SetLocale::class]); + })->middleware(['web']); }); $response = $this->get('some/route'); diff --git a/tests/TestCase.php b/tests/TestCase.php index 5f29dc2..25892ec 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -21,6 +21,24 @@ protected function setUp(): void Config::set('app.key', Str::random(32)); } + /** + * Resolve application Console Kernel implementation. + * + * @param \Illuminate\Foundation\Application $app + * + * @return void + */ + protected function resolveApplicationHttpKernel($app): void + { + // In Laravel 6+, we need to add the middleware to + // $middlewarePriority in Kernel.php for route + // model binding to work properly. + $app->singleton( + 'Illuminate\Contracts\Http\Kernel', + 'CodeZero\Localizer\Tests\Stubs\Kernel' + ); + } + /** * Get the packages service providers. * diff --git a/tests/stubs/Kernel.php b/tests/stubs/Kernel.php new file mode 100644 index 0000000..691f8f8 --- /dev/null +++ b/tests/stubs/Kernel.php @@ -0,0 +1,25 @@ + [ + \Illuminate\Cookie\Middleware\EncryptCookies::class, + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + \Illuminate\Session\Middleware\StartSession::class, + // \Illuminate\Session\Middleware\AuthenticateSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \CodeZero\Localizer\Middleware\SetLocale::class, // <== Added Middleware Here + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ], + ]; +} From 7879082ed7be5c5123a7c0df228edec550156489 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Fri, 18 Aug 2023 12:35:42 +0200 Subject: [PATCH 2/5] Delete Kernel.php --- tests/stubs/Kernel.php | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 tests/stubs/Kernel.php diff --git a/tests/stubs/Kernel.php b/tests/stubs/Kernel.php deleted file mode 100644 index 691f8f8..0000000 --- a/tests/stubs/Kernel.php +++ /dev/null @@ -1,25 +0,0 @@ - [ - \Illuminate\Cookie\Middleware\EncryptCookies::class, - \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, - \Illuminate\Session\Middleware\StartSession::class, - // \Illuminate\Session\Middleware\AuthenticateSession::class, - \Illuminate\View\Middleware\ShareErrorsFromSession::class, - \CodeZero\Localizer\Middleware\SetLocale::class, // <== Added Middleware Here - \Illuminate\Routing\Middleware\SubstituteBindings::class, - ], - ]; -} From 563fc341477be64a37fa96d2ffdd1b79c4dd60ed Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Fri, 18 Aug 2023 12:35:53 +0200 Subject: [PATCH 3/5] Create Kernel.php --- tests/Stubs/Kernel.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/Stubs/Kernel.php diff --git a/tests/Stubs/Kernel.php b/tests/Stubs/Kernel.php new file mode 100644 index 0000000..691f8f8 --- /dev/null +++ b/tests/Stubs/Kernel.php @@ -0,0 +1,25 @@ + [ + \Illuminate\Cookie\Middleware\EncryptCookies::class, + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + \Illuminate\Session\Middleware\StartSession::class, + // \Illuminate\Session\Middleware\AuthenticateSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \CodeZero\Localizer\Middleware\SetLocale::class, // <== Added Middleware Here + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ], + ]; +} From 174f179e25b37766dedfc1d608e5eca981dc8fee Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Fri, 18 Aug 2023 12:38:37 +0200 Subject: [PATCH 4/5] Update README.md --- README.md | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 74010da..7ed5e64 100644 --- a/README.md +++ b/README.md @@ -45,27 +45,15 @@ Make sure to add it after `StartSession` and before `SubstituteBindings`: ```php protected $middlewareGroups = [ 'web' => [ + //... + \Illuminate\Session\Middleware\StartSession::class, // <= after this //... \CodeZero\Localizer\Middleware\SetLocale::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this ], ]; ``` -You also need to add the middleware to the `$middlewarePriority` array in `app/Http/Kernel.php` -to trigger it in the correct order: - -```php -protected $middlewarePriority = [ - \Illuminate\Session\Middleware\StartSession::class, // <= after this - //... - \CodeZero\Localizer\Middleware\SetLocale::class, - \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this -]; -``` - -If you don't see the `$middlewarePriority` array in your kernel file, -then you can copy it over from the parent class `Illuminate\Foundation\Http\Kernel`. - ## ⚙ Configure ### Publish Configuration File From a0390c757dadf7d2e77689ab0cf928f9c108a892 Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Sat, 2 Mar 2024 14:22:30 +0100 Subject: [PATCH 5/5] Update middleware instructions in README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ed5e64..4be467e 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,10 @@ Laravel will automatically register the ServiceProvider. ## 🧩 Add Middleware Add the middleware to the `web` middleware group in `app/Http/Kernel.php`. -Make sure to add it after `StartSession` and before `SubstituteBindings`: +Make sure to add it after `StartSession` and before `SubstituteBindings`. + +The order of the middleware is important if you are using localized route keys (translated slugs)! +The session needs to be active when setting the locale, and the locale needs to be set when substituting the route bindings. ```php protected $middlewareGroups = [