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 23, 2024
1 parent 454a363 commit b386aa7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
22 changes: 13 additions & 9 deletions app/Http/Controllers/FallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
namespace Blumilk\Website\Http\Controllers;

use CodeZero\LocalizedRoutes\Controllers\FallbackController as OriginalFallbackController;
use CodeZero\LocalizedRoutes\LocalizedUrlGenerator;
use CodeZero\UriTranslator\UriTranslator;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
use Illuminate\Translation\Translator;
use Symfony\Component\HttpFoundation\RedirectResponse;

class FallbackController extends OriginalFallbackController
{
public function __construct(
protected Translator $translator,
protected Request $request,
protected Router $router,
protected LocalizedUrlGenerator $urlGenerator,
protected UriTranslator $translator,
) {}

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

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

if ($route->isFallback) {
Expand All @@ -34,14 +38,14 @@ protected function redirectResponse()
->header("Cache-Control", "no-store, no-cache, must-revalidate");
}

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

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

return Route::getRoutes()->match(Request::create($domain . "/" . $url));
return $this->router->getRoutes()->match(Request::create($domain . "/" . $url));
}
}
7 changes: 5 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\Router;

Route::middleware("auth:sanctum")->get("/user", fn(Request $request): JsonResponse => new JsonResponse($request->user()));
/** @var Router $router */
$router = app(Router::class);

$router->middleware("auth:sanctum")->get("/user", fn(Request $request): JsonResponse => new JsonResponse($request->user()));

0 comments on commit b386aa7

Please sign in to comment.