| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Providers;
- 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
- {
- /**
- * 应用程序的“主”路径。
- *
- * This is used by Laravel authentication to redirect users after login.
- * Laravel身份验证使用它在用户登录后重定向用户
- *
- * @var string
- */
- public const HOME = '/home';
- /**
- * 应用程序的控制器命名空间
- *
- * 当存在时,控制器路由声明将自动以该名称空间作为前缀。
- *
- * @var string|null
- */
- // protected $namespace = 'App\\Http\\Controllers';
- /**
- * 定义路由模型绑定、模式过滤器等
- *
- * @return void
- */
- public function boot()
- {
- // 为应用程序配置速率限制器
- $this->configureRateLimiting();
- // 注册将用于加载应用程序路由的回调。
- $this->routes(function () {
- // api路由
- Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
- });
- }
- /**
- * 为应用程序配置速率限制器
- *
- * @return void
- */
- protected function configureRateLimiting()
- {
- RateLimiter::for('api', function (Request $request) {
- return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
- });
- }
- }
|