RouteServiceProvider.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Providers;
  4. use Illuminate\Cache\RateLimiting\Limit;
  5. use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\RateLimiter;
  8. use Illuminate\Support\Facades\Route;
  9. use Str;
  10. class RouteServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * The path to the "home" route for your application.
  14. *
  15. * Typically, users are redirected here after authentication.
  16. *
  17. * @var string
  18. */
  19. public const HOME = '/home';
  20. /**
  21. * Define your route model bindings, pattern filters, etc.
  22. *
  23. * @return void
  24. */
  25. public function boot()
  26. {
  27. $this->configureRateLimiting();
  28. $this->routes(function () {
  29. Route::middleware('api')
  30. ->prefix('api')
  31. ->namespace($this->namespace)
  32. ->group(base_path('routes/api.php'));
  33. Route::middleware('web')
  34. ->namespace($this->namespace)
  35. ->group(base_path('routes/web.php'));
  36. Route::middleware('web')
  37. ->group(base_path('routes/redirect.php'));
  38. });
  39. // This will remove the index.php from the URL and prevent canonical conflicts.
  40. if (Str::contains(request()->getRequestUri(), '/index.php/')) {
  41. $url = str_replace('index.php/', '', request()->getRequestUri());
  42. if ($url !== '') {
  43. header("Location: $url", true, 301);
  44. exit;
  45. }
  46. }
  47. // The `id` must be an integer
  48. Route::pattern('id', '[0-9]+');
  49. }
  50. /**
  51. * Configure the rate limiters for the application.
  52. *
  53. * @return void
  54. */
  55. protected function configureRateLimiting()
  56. {
  57. RateLimiter::for('api', function (Request $request) {
  58. return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
  59. });
  60. }
  61. }