RouteServiceProvider.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Cache\RateLimiting\Limit;
  4. use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\RateLimiter;
  7. use Illuminate\Support\Facades\Route;
  8. /**
  9. * 路由服务提供者
  10. *
  11. */
  12. class RouteServiceProvider extends ServiceProvider
  13. {
  14. /**
  15. * 应用程序的“主”路径。
  16. *
  17. * This is used by Laravel authentication to redirect users after login.
  18. * Laravel身份验证使用它在用户登录后重定向用户
  19. *
  20. * @var string
  21. */
  22. public const HOME = '/home';
  23. /**
  24. * 应用程序的控制器命名空间
  25. *
  26. * 当存在时,控制器路由声明将自动以该名称空间作为前缀。
  27. *
  28. * @var string|null
  29. */
  30. // protected $namespace = 'App\\Http\\Controllers';
  31. /**
  32. * 定义路由模型绑定、模式过滤器等
  33. *
  34. * @return void
  35. */
  36. public function boot()
  37. {
  38. // 为应用程序配置速率限制器
  39. $this->configureRateLimiting();
  40. // 注册将用于加载应用程序路由的回调。
  41. $this->routes(function () {
  42. // api路由
  43. Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
  44. });
  45. }
  46. /**
  47. * 为应用程序配置速率限制器
  48. *
  49. * @return void
  50. */
  51. protected function configureRateLimiting()
  52. {
  53. RateLimiter::for('api', function (Request $request) {
  54. return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
  55. });
  56. }
  57. }