12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?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'));
- // web路由
- Route::middleware('web')
- ->namespace($this->namespace)
- ->group(base_path('routes/web.php'));
- });
- }
- /**
- * 为应用程序配置速率限制器
- *
- * @return void
- */
- protected function configureRateLimiting()
- {
- RateLimiter::for('api', function (Request $request) {
- return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
- });
- }
- }
|