From 7324f4aa217e3ff2e6d29cc6df7cd286d6937a2b Mon Sep 17 00:00:00 2001 From: Ivan Vermeyen Date: Sat, 2 Mar 2024 14:26:27 +0100 Subject: [PATCH] Simplify middleware priority (#18) --- README.md | 23 +++++++-------------- tests/Feature/SetLocaleTest.php | 36 ++++++++++++++++----------------- tests/Stubs/Kernel.php | 25 +++++++++++++++++++++++ tests/TestCase.php | 18 +++++++++++++++++ 4 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 tests/Stubs/Kernel.php diff --git a/README.md b/README.md index 74010da..4be467e 100644 --- a/README.md +++ b/README.md @@ -40,32 +40,23 @@ 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 = [ '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 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/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, + ], + ]; +} 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. *