ldc-dam/app/Providers/RouteServiceProvider.php
Jérémy Munsch 14aeec5e2a Fixes #10
- Remove explicition resolve for media in RouteServicePRovider
- Set usage of media id on media routes everywhere
2025-05-06 11:02:02 +02:00

85 lines
2.5 KiB
PHP

<?php
namespace App\Providers;
use App\Enums\MediaStatut;
use App\Models\Article;
use App\Models\Media;
use App\Models\Panier;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
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 = '/';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->group(base_path('routes/web.php'));
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
});
Route::bind('basket_uuid', function ($value) {
return Panier::where('uid', $value)
->with('owner')->firstOrFail();
});
// * API
Route::bind('apiNoArticle', function ($value) {
return Article::where('noArt', $value)->firstOrFail();
});
Route::bind('apiMedia', function ($value) {
return Media::where(function ($q) use ($value) {
$q->where('id', $value);
})->whereIn('statut', [
MediaStatut::public,
MediaStatut::public_etendu
])->firstOrFail();
});
Route::bind('apiMediaArcadiaId', function ($value) {
return Media::where(function ($q) use ($value) {
$q->where('arcadia_id', $value)
->whereNotNull('arcadia_id');
})->whereIn('statut', [
MediaStatut::public,
MediaStatut::public_etendu
])->firstOrFail();
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(config('api.apiRequestRatePerMinute', 100))
->by($request->user()?->id ?: app('clientIp'));
});
}
}