Skip to content

Commit

Permalink
Adding route groups with a locale prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 14, 2024
1 parent 5e54e0b commit e3563e5
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Facades/LocalizationRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Facades;

use Closure;
use Illuminate\Support\Facades\Facade;
use LaravelLang\Routes\Services\Route;

/**
* @method static void group(Closure $callback)
*/
class LocalizationRoute extends Facade
{
protected static function getFacadeAccessor(): string
{
return Route::class;
}
}
34 changes: 34 additions & 0 deletions src/Services/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Routes\Services;

use Closure;
use Illuminate\Support\Facades\Route as BaseRoute;
use LaravelLang\Routes\Concerns\KeyNames;
use LaravelLang\Routes\Middlewares\LocalizationByCookie;
use LaravelLang\Routes\Middlewares\LocalizationByHeader;
use LaravelLang\Routes\Middlewares\LocalizationByParameter;
use LaravelLang\Routes\Middlewares\LocalizationBySession;

use function sprintf;

class Route
{
use KeyNames;

public function group(Closure $callback): void
{
BaseRoute::prefix(sprintf('{%s}', $this->names()->parameter))
->name($this->names()->parameter . '.')
->middleware(LocalizationByParameter::class)
->group($callback);

BaseRoute::middleware([
LocalizationByCookie::class,
LocalizationByHeader::class,
LocalizationBySession::class,
])->group($callback);
}
}
8 changes: 8 additions & 0 deletions tests/Concerns/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Concerns;

use Closure;
use LaravelLang\Routes\Facades\LocalizationRoute;
use LaravelLang\Routes\Middlewares\LocalizationByCookie;
use LaravelLang\Routes\Middlewares\LocalizationByHeader;
use LaravelLang\Routes\Middlewares\LocalizationByParameter;
Expand Down Expand Up @@ -63,6 +64,13 @@ public function setUpRoutes(): void
->get('clean/{foo}', $this->jsonResponse())
->name('clean');
});

LocalizationRoute::group(function () {
app('router')
->middleware('web')
->get('grouped', $this->jsonResponse())
->name('via.group');
});
}

protected function jsonResponse(): Closure
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/Facades/RouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use LaravelLang\Routes\Helpers\Name;

test('group', function () {
expect(app('router')->has('via.group'))->toBeTrue();
expect(app('router')->has(Name::parameter() . '.via.group'))->toBeTrue();
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e3563e5

Please sign in to comment.