Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify middleware priority #18

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions tests/Feature/SetLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');
Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');
Expand Down
25 changes: 25 additions & 0 deletions tests/Stubs/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace CodeZero\Localizer\Tests\Stubs;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\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,
],
];
}
18 changes: 18 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading