Skip to content

Commit

Permalink
#198 - i18n routing
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofrewak committed Jan 5, 2024
1 parent c0661f3 commit 454a363
Show file tree
Hide file tree
Showing 43 changed files with 679 additions and 418 deletions.
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Console;
namespace Blumilk\Website\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Exceptions;
namespace Blumilk\Website\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

Expand Down
16 changes: 16 additions & 0 deletions app/Http/Controllers/ContactController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Http\Controllers;

use Inertia\Response;
use Inertia\ResponseFactory;

class ContactController extends Controller
{
public function __invoke(ResponseFactory $factory): Response
{
return $factory->render("Contact");
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Http\Controllers;
namespace Blumilk\Website\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
Expand Down
47 changes: 47 additions & 0 deletions app/Http/Controllers/FallbackController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Http\Controllers;

use CodeZero\LocalizedRoutes\Controllers\FallbackController as OriginalFallbackController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
use Illuminate\Translation\Translator;

class FallbackController extends OriginalFallbackController
{
public function __construct(
protected Translator $translator,
protected Request $request,
) {}

protected function redirectResponse()
{
if (!$this->shouldRedirect()) {
return false;
}

$localizedUrl = Route::localizedUrl();
$route = $this->findRouteByUrl($localizedUrl);

if ($route->isFallback) {
return false;
}

return Redirect::to($route->uri(), $this->getRedirectStatusCode())
->header("Cache-Control", "no-store, no-cache, must-revalidate");
}

protected function findRouteByUrl(string $url): \Illuminate\Routing\Route
{
$parts = explode("/", $url);
$path = implode("/", array_splice($parts, 4));
$url = $this->translator->uri($path);

$domain = implode("/", array_splice($parts, -4));

return Route::getRoutes()->match(Request::create($domain . "/" . $url));
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Http\Controllers;

use Inertia\Response;
use Inertia\ResponseFactory;

class HomeController extends Controller
{
public function __invoke(ResponseFactory $factory): Response
{
return $factory->render("Home");
}
}
24 changes: 14 additions & 10 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,39 @@

declare(strict_types=1);

namespace BlumilkWebsite\Http;
namespace Blumilk\Website\Http;

use BlumilkWebsite\Http\Middleware\Authenticate;
use BlumilkWebsite\Http\Middleware\EncryptCookies;
use BlumilkWebsite\Http\Middleware\HandleInertiaRequests;
use BlumilkWebsite\Http\Middleware\PreventRequestsDuringMaintenance;
use BlumilkWebsite\Http\Middleware\RedirectIfAuthenticated;
use BlumilkWebsite\Http\Middleware\TrimStrings;
use BlumilkWebsite\Http\Middleware\TrustProxies;
use BlumilkWebsite\Http\Middleware\ValidateSignature;
use BlumilkWebsite\Http\Middleware\VerifyCsrfToken;
use Blumilk\Website\Http\Middleware\Authenticate;
use Blumilk\Website\Http\Middleware\Localize;
use Blumilk\Website\Http\Middleware\RedirectIfAuthenticated;
use Blumilk\Website\Http\Middleware\TrimStrings;
use Blumilk\Website\Http\Middleware\TrustProxies;
use CodeZero\LocalizedRoutes\Middleware\SetLocale;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Auth\Middleware\EnsureEmailIsVerified;
use Illuminate\Auth\Middleware\RequirePassword;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance;
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Http\Middleware\HandleCors;
use Illuminate\Http\Middleware\SetCacheHeaders;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Routing\Middleware\ValidateSignature;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use Inertia\Middleware as HandleInertiaRequests;

class Kernel extends HttpKernel
{
protected $middleware = [
Localize::class,
TrustProxies::class,
HandleCors::class,
PreventRequestsDuringMaintenance::class,
Expand All @@ -46,6 +49,7 @@ class Kernel extends HttpKernel
StartSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SetLocale::class,
SubstituteBindings::class,
HandleInertiaRequests::class,
],
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Http\Middleware;
namespace Blumilk\Website\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
Expand Down
12 changes: 0 additions & 12 deletions app/Http/Middleware/EncryptCookies.php

This file was deleted.

18 changes: 0 additions & 18 deletions app/Http/Middleware/HandleInertiaRequests.php

This file was deleted.

21 changes: 21 additions & 0 deletions app/Http/Middleware/Localize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Blumilk\Website\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class Localize
{
public function handle(Request $request, Closure $next): Response
{
if (str_starts_with($request->header("Accept-Language"), "pl")) {
app()->setLocale("pl");
}

return $next($request);
}
}
13 changes: 0 additions & 13 deletions app/Http/Middleware/PreventRequestsDuringMaintenance.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace BlumilkWebsite\Http\Middleware;
namespace Blumilk\Website\Http\Middleware;

use Blumilk\Website\Providers\RouteServiceProvider;
use Closure;
use BlumilkWebsite\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/TrimStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Http\Middleware;
namespace Blumilk\Website\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/TrustHosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Http\Middleware;
namespace Blumilk\Website\Http\Middleware;

use Illuminate\Http\Middleware\TrustHosts as Middleware;

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Http\Middleware;
namespace Blumilk\Website\Http\Middleware;

use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;
Expand Down
12 changes: 0 additions & 12 deletions app/Http/Middleware/ValidateSignature.php

This file was deleted.

12 changes: 0 additions & 12 deletions app/Http/Middleware/VerifyCsrfToken.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Models;
namespace Blumilk\Website\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Providers;
namespace Blumilk\Website\Providers;

use Illuminate\Support\ServiceProvider;

Expand Down
2 changes: 1 addition & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace BlumilkWebsite\Providers;
namespace Blumilk\Website\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
Expand Down
11 changes: 8 additions & 3 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace BlumilkWebsite\Providers;
namespace Blumilk\Website\Providers;

use Blumilk\Website\Http\Controllers\FallbackController;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
Expand All @@ -23,8 +24,12 @@ public function boot(): void
->prefix("api")
->group(base_path("routes/api.php"));

Route::middleware("web")
->group(base_path("routes/web.php"));
Route::localized(function (): void {
Route::middleware("web")
->group(base_path("routes/web.php"));
});

Route::fallback(FallbackController::class);
});
}
}
6 changes: 3 additions & 3 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use BlumilkWebsite\Console\Kernel as ConsoleKernel;
use BlumilkWebsite\Exceptions\Handler;
use BlumilkWebsite\Http\Kernel as HttpKernel;
use Blumilk\Website\Console\Kernel as ConsoleKernel;
use Blumilk\Website\Exceptions\Handler;
use Blumilk\Website\Http\Kernel as HttpKernel;
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Http\Kernel as HttpKernelContract;
Expand Down
Loading

0 comments on commit 454a363

Please sign in to comment.