Skip to content

Commit addc443

Browse files
Merge pull request #20 from Laravel-Lang/1.x
Replaced the UrlGenerator override with the `localizedRoute` method
2 parents d7fcbe8 + 644c389 commit addc443

File tree

7 files changed

+76
-98
lines changed

7 files changed

+76
-98
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@
4747
"autoload": {
4848
"psr-4": {
4949
"LaravelLang\\Routes\\": "src/"
50-
}
50+
},
51+
"files": [
52+
"helpers/functions.php"
53+
]
5154
},
5255
"autoload-dev": {
5356
"psr-4": {

helpers/functions.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Support\Facades\Route;
6+
use Illuminate\Support\Str;
7+
use LaravelLang\Config\Facades\Config;
8+
9+
if (! function_exists('localizedRoute')) {
10+
function localizedRoute(string $route, array $parameters = [], bool $absolute = true): string
11+
{
12+
$locale = Config::shared()->routes->names->parameter;
13+
$prefix = Config::shared()->routes->namePrefix;
14+
15+
$name = Str::start($route, $prefix);
16+
17+
if (! Route::has($name)) {
18+
return route($route, $parameters, $absolute);
19+
}
20+
21+
return route($name, array_merge([
22+
$locale => app()->getLocale(),
23+
], $parameters), $absolute);
24+
}
25+
}

ide.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
3+
"completions": [
4+
{
5+
"complete": "routeName",
6+
"condition": [
7+
{
8+
"functionNames": [
9+
"localizedRoute"
10+
],
11+
"parameters": [
12+
1
13+
]
14+
}
15+
]
16+
}
17+
]
18+
}

src/ServiceProvider.php

-24
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,19 @@
55
namespace LaravelLang\Routes;
66

77
use Closure;
8-
use Illuminate\Foundation\Application;
9-
use Illuminate\Http\Request;
108
use Illuminate\Support\Facades\Route;
119
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
1210
use LaravelLang\Routes\Facades\LocalizationRoute;
13-
use LaravelLang\Routes\Services\UrlGenerator;
1411

1512
class ServiceProvider extends BaseServiceProvider
1613
{
1714
public function boot(): void
1815
{
1916
$this->registerGroup();
20-
$this->urlGenerator();
2117
}
2218

2319
protected function registerGroup(): void
2420
{
2521
Route::macro('localizedGroup', fn (Closure $callback) => LocalizationRoute::group($callback));
2622
}
27-
28-
protected function urlGenerator(): void
29-
{
30-
$this->app->singleton('url', function ($app) {
31-
$routes = $app['router']->getRoutes();
32-
33-
$app->instance('routes', $routes);
34-
35-
return new UrlGenerator(
36-
$routes,
37-
$app->rebinding('request', $this->requestRebinder()),
38-
$app['config']['app.asset_url']
39-
);
40-
});
41-
}
42-
43-
protected function requestRebinder(): Closure
44-
{
45-
return fn (Application $app, Request $request) => $app['url']->setRequest($request);
46-
}
4723
}

src/Services/UrlGenerator.php

-47
This file was deleted.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use LaravelLang\Config\Facades\Config;
6+
use Tests\Constants\LocaleValue;
7+
8+
test('route groups', function () {
9+
$name = Config::shared()->routes->namePrefix;
10+
$locale = LocaleValue::LocaleMain;
11+
$fallback = LocaleValue::LocaleAliasParent;
12+
13+
expect(localizedRoute($name . 'via.group.facade', ['foo' => 'bar']))
14+
->toEndWith("localhost/$locale/group/facade/bar");
15+
16+
expect(localizedRoute($name . 'via.group.macro', ['foo' => 'bar', 'locale' => $fallback]))
17+
->toEndWith("localhost/$fallback/group/macro/bar");
18+
19+
expect(localizedRoute('via.group.facade', ['foo' => 'bar']))
20+
->toEndWith("localhost/$locale/group/facade/bar");
21+
22+
expect(localizedRoute('via.group.macro', ['foo' => 'bar', 'locale' => $fallback]))
23+
->toEndWith("localhost/$fallback/group/macro/bar");
24+
});
25+
26+
test('routes without groups', function () {
27+
expect(localizedRoute('via.model.default', ['foo' => 'bar']))
28+
->toEndWith('localhost/model/default/bar');
29+
});

tests/Unit/Services/UrlGeneratorTest.php

-26
This file was deleted.

0 commit comments

Comments
 (0)