76 lines
2.6 KiB
PHP
76 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\Gamme;
|
|
use App\Models\Product;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
class RouteServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* The path to the "home" route for your application.
|
|
*
|
|
* This is used by Laravel authentication to redirect users after login.
|
|
*
|
|
* @var string
|
|
*/
|
|
public const HOME = '/bo';
|
|
|
|
/**
|
|
* Define your route model bindings, pattern filters, etc.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->routes(function () {
|
|
Route::middleware('web')
|
|
->namespace($this->namespace)
|
|
->group(base_path('routes/web.php'));
|
|
});
|
|
|
|
Route::bind('gammeSlug', function ($value) {
|
|
/** @var string $locale @var string $defaultLocale @var string $value */
|
|
$vars = $this->getRouteBinderVars($value);
|
|
\extract($vars);
|
|
/** @var \App\Models\Gamme */
|
|
$gamme = Gamme::where(function (Builder $q) use ($locale, $defaultLocale, $value) {
|
|
$q->whereRaw("JSON_EXTRACT(slug, '$.$locale') = '$value'")
|
|
->orWhereRaw("JSON_EXTRACT(slug, '$.$defaultLocale') = '$value'");
|
|
})->firstOrFail();
|
|
return $gamme->setLocale($locale);
|
|
});
|
|
|
|
Route::bind('productSlug', function ($value, $route) {
|
|
/** @var \Illuminate\Routing\Route $route */
|
|
$gamme = $route->parameter('gammeSlug');
|
|
|
|
/** @var string $locale @var string $defaultLocale @var string $value */
|
|
$vars = $this->getRouteBinderVars($value);
|
|
\extract($vars);
|
|
/** @var \App\Models\Product */
|
|
$product = Product::where(function (Builder $q) use ($locale, $defaultLocale, $value) {
|
|
$q->whereRaw("JSON_EXTRACT(slug, '$.$locale') = '$value'")
|
|
->orWhereRaw("JSON_EXTRACT(slug, '$.$defaultLocale') = '$value'");
|
|
})->where('gamme_id', $gamme->id)
|
|
->firstOrFail();
|
|
return $product->setLocale($locale);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get route binder common
|
|
* @param string $value
|
|
* @return string[]
|
|
*/
|
|
private function getRouteBinderVars(string $value): array
|
|
{
|
|
$locale = \app()->getLocale();
|
|
$defaultLocale = \config('translatable.fallback_locale');
|
|
$value = \filter_var($value, \FILTER_SANITIZE_STRING);
|
|
return compact('locale', 'defaultLocale', 'value');
|
|
}
|
|
}
|