| 1 | <?php | = | 1 | <?php |
| 2 | 2 | |||
| 3 | namespace App\Providers; | 3 | namespace App\Providers; | |
| 4 | 4 | |||
| -+ | 5 | use Illuminate\Cache\RateLimiting\Limit; | ||
| 5 | use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; | = | 6 | use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; |
| -+ | 7 | use Illuminate\Http\Request; | ||
| 8 | use Illuminate\Support\Facades\RateLimiter; | |||
| 6 | use Illuminate\Support\Facades\Route; | = | 9 | use Illuminate\Support\Facades\Route; |
| 7 | 10 | |||
| 8 | class RouteServiceProvider extends ServiceProvider | 11 | class RouteServiceProvider extends ServiceProvider | |
| 9 | { | 12 | { | |
| 10 | /** | 13 | /** | |
| 11 | * This namespace is applied to your controller routes. | <> | 14 | * The path to the "home" route for your application. |
| 12 | * | = | 15 | * |
| 13 | * In addition, it is set as the URL generator's root namespace. | <> | 16 | * This is used by Laravel authentication to redirect users after login. |
| 14 | * | = | 17 | * |
| 15 | * @var string | 18 | * @var string | |
| 16 | */ | 19 | */ | |
| 17 | protected $namespace = 'App\Http\Controllers'; | <> | 20 | public const HOME = '/home'; |
| 18 | = | 21 | ||
| 19 | /** | 22 | /** | |
| 20 | * The path to the "home" route for your application. | <> | 23 | * The controller namespace for the application. |
| 21 | * | = | 24 | * |
| <> | 25 | * When present, controller route declarations will automatically be prefixed with this namespace. | ||
| 26 | * | |||
| 22 | * @var string | 27 | * @var string|null | |
| 23 | */ | = | 28 | */ |
| 24 | public const HOME = '/home'; | <> | 29 | // protected $namespace = 'App\\Http\\Controllers'; |
| 25 | = | 30 | ||
| 26 | /** | 31 | /** | |
| 27 | * Define your route model bindings, pattern filters, etc. | 32 | * Define your route model bindings, pattern filters, etc. | |
| 28 | * | 33 | * | |
| 29 | * @return void | 34 | * @return void | |
| 30 | */ | 35 | */ | |
| 31 | public function boot() | 36 | public function boot() | |
| 32 | { | 37 | { | |
| 33 | // | <> | ||
| 34 | ||||
| 35 | parent::boot(); | |||
| 36 | } | |||
| 37 | ||||
| 38 | /** | |||
| 39 | * Define the routes for the application. | |||
| 40 | * | |||
| 41 | * @return void | |||
| 42 | */ | |||
| 43 | public function map() | |||
| 44 | { | |||
| 45 | $this->mapApiRoutes(); | 38 | $this->configureRateLimiting(); | |
| 46 | = | 39 | ||
| 47 | $this->mapWebRoutes(); | <> | 40 | $this->routes(function () { |
| 48 | 41 | Route::prefix('api') | ||
| 49 | // | 42 | ->middleware('api') | |
| 50 | } | |||
| 51 | ||||
| 52 | /** | |||
| 53 | * Define the "web" routes for the application. | |||
| 54 | * | |||
| 55 | * These routes all receive session state, CSRF protection, etc. | |||
| 56 | * | |||
| 57 | * @return void | |||
| 58 | */ | |||
| 59 | protected function mapWebRoutes() | 43 | ->namespace($this->namespace) | |
| 44 | ->group(base_path('routes/api.php')); | |||
| 60 | { | 45 | ||
| 61 | Route::middleware('web') | 46 | Route::middleware('web') | |
| 62 | ->namespace($this->namespace) | 47 | ->namespace($this->namespace) | |
| 63 | ->group(base_path('routes/web.php')); | 48 | ->group(base_path('routes/web.php')); | |
| 49 | }); | |||
| 64 | } | = | 50 | } |
| 65 | 51 | |||
| 66 | /** | 52 | /** | |
| 67 | * Define the "api" routes for the application. | <> | 53 | * Configure the rate limiters for the application. |
| 68 | * | = | 54 | * |
| 69 | * These routes are typically stateless. | +- | ||
| 70 | * | |||
| 71 | * @return void | = | 55 | * @return void |
| 72 | */ | 56 | */ | |
| 73 | protected function mapApiRoutes() | <> | 57 | protected function configureRateLimiting() |
| 74 | { | = | 58 | { |
| 75 | Route::prefix('api') | <> | 59 | RateLimiter::for('api', function (Request $request) { |
| 76 | ->middleware('api') | 60 | return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip()); | |
| 77 | ->namespace($this->namespace) | 61 | }); | |
| 78 | ->group(base_path('routes/api.php')); | |||
| 79 | } | = | 62 | } |
| 80 | } | 63 | } |