RouteServiceProvider.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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')
  44. ->middleware('api')
  45. ->namespace($this->namespace)
  46. ->group(base_path('routes/api.php'));
  47. // web路由
  48. Route::middleware('web')
  49. ->namespace($this->namespace)
  50. ->group(base_path('routes/web.php'));
  51. });
  52. }
  53. /**
  54. * 为应用程序配置速率限制器
  55. *
  56. * @return void
  57. */
  58. protected function configureRateLimiting()
  59. {
  60. RateLimiter::for('api', function (Request $request) {
  61. return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
  62. });
  63. }
  64. }