Last active
October 26, 2025 23:13
-
-
Save devhammed/09ca128988abe8dbf5cb0faa802a4730 to your computer and use it in GitHub Desktop.
Laravel Locale-aware URL generator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Routing; | |
| use Illuminate\Routing\UrlGenerator as BaseUrlGenerator; | |
| use Mcamara\LaravelLocalization\Facades\LaravelLocalization; | |
| class UrlGenerator extends BaseUrlGenerator | |
| { | |
| public function to($path, $extra = [], $secure = null): string | |
| { | |
| return $this->prefix(parent::to($path, $extra, $secure)); | |
| } | |
| public function route($name, $parameters = [], $absolute = true): string | |
| { | |
| return $this->prefix(parent::route($name, $parameters, $absolute)); | |
| } | |
| public function toRoute($route, $parameters, $absolute): string | |
| { | |
| return $this->prefix(parent::toRoute($route, $parameters, $absolute)); | |
| } | |
| protected function prefix(string $url): string | |
| { | |
| $locale = LaravelLocalization::setLocale(); | |
| if ($locale === null) { | |
| return $url; | |
| } | |
| $absolute = str_starts_with($url, 'http://') || str_starts_with($url, 'https://'); | |
| $appUrl = config('app.url'); | |
| if ( ! $absolute) { | |
| $url = "$appUrl$url"; | |
| } | |
| $parsedAppUrl = parse_url($appUrl); | |
| $parsedUrl = parse_url($url); | |
| $appHost = $parsedAppUrl['host'] ?? null; | |
| $urlHost = $parsedUrl['host'] ?? null; | |
| if ($appHost === null || $urlHost === null || $appHost !== $urlHost) { | |
| return $url; | |
| } | |
| $prefix = '/'.$locale; | |
| $path = $parsedUrl['path'] ?? '/'; | |
| if ( ! str_starts_with($path, $prefix)) { | |
| $parsedUrl['path'] = $prefix.'/'.ltrim($path, '/'); | |
| } | |
| $completePath = ($parsedUrl['path'] ?? '') | |
| .(isset($parsedUrl['query']) ? "?{$parsedUrl['query']}" : '') | |
| .(isset($parsedUrl['fragment']) ? "#{$parsedUrl['fragment']}" : ''); | |
| if ( ! $absolute) { | |
| return $completePath; | |
| } | |
| return (isset($parsedUrl['scheme']) ? "{$parsedUrl['scheme']}://" : '') | |
| .(isset($parsedUrl['user']) ? ($parsedUrl['user'].(isset($parsedUrl['pass']) ? ":{$parsedUrl['pass']}" : '').'@') : '') | |
| .($parsedUrl['host'] ?? '') | |
| .(isset($parsedUrl['port']) ? ":{$parsedUrl['port']}" : '') | |
| .$completePath; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Providers; | |
| use Carbon\CarbonImmutable; | |
| use App\Support\Routing\UrlGenerator; | |
| use Illuminate\Database\Eloquent\Model; | |
| use Illuminate\Database\Eloquent\Relations\Relation; | |
| use Illuminate\Support\Facades\Date; | |
| use Illuminate\Support\ServiceProvider; | |
| class RouteServiceProvider extends ServiceProvider | |
| { | |
| /** | |
| * Register any application services. | |
| */ | |
| public function register(): void | |
| { | |
| $this->app->extend('url', function ($service, $app) { | |
| $routes = $app['router']->getRoutes(); | |
| $generator = new UrlGenerator( | |
| $routes, | |
| $app->rebinding('request', function ($app, $request) use (&$generator) { | |
| $generator->setRequest($request); | |
| }), | |
| ); | |
| $generator->setSessionResolver(fn() => $app['session'] ?? null); | |
| $generator->setKeyResolver(fn() => $app['config']['app.key']); | |
| return $generator; | |
| }); | |
| } | |
| /** | |
| * Bootstrap any application services. | |
| */ | |
| public function boot(): void | |
| { | |
| // | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment