RouteServiceProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. class RouteServiceProvider extends ServiceProvider
  9. {
  10. /**
  11. * The path to the "home" route for your application.
  12. *
  13. * This is used by Laravel authentication to redirect users after login.
  14. *
  15. * @var string
  16. */
  17. public const HOME = '/home';
  18. /**
  19. * The controller namespace for the application.
  20. *
  21. * When present, controller route declarations will automatically be prefixed with this namespace.
  22. *
  23. * @var string|null
  24. */
  25. protected $namespace = 'App\\Http\\Controllers';
  26. /**
  27. * Define your route model bindings, pattern filters, etc.
  28. *
  29. * @return void
  30. */
  31. public function boot()
  32. {
  33. $this->configureRateLimiting();
  34. $this->mapApiRoutes();
  35. $this->mapWpsRoutes();
  36. $this->mapAdminApiRoutes();
  37. $this->routes(function () {
  38. Route::middleware('web')
  39. ->namespace($this->namespace)
  40. ->group(base_path('routes/web.php'));
  41. });
  42. }
  43. /**
  44. * Configure the rate limiters for the application.
  45. *
  46. * @return void
  47. */
  48. protected function configureRateLimiting()
  49. {
  50. RateLimiter::for('api', function (Request $request) {
  51. return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
  52. });
  53. }
  54. protected function mapApiRoutes()
  55. {
  56. Route::prefix('api')
  57. ->middleware('api')
  58. ->namespace($this->namespace)
  59. ->group(base_path('routes/api.php'));
  60. //dd($this->namespace);
  61. foreach ( glob(base_path('routes'.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR).'*.php' ) as $fileName ) {
  62. $fileName = basename( $fileName );
  63. Route::middleware('api_verify')
  64. ->namespace( $this->namespace."\\Api" )
  65. ->prefix('api')
  66. ->group( base_path('routes'.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR."{$fileName}") );
  67. }
  68. }
  69. /**
  70. * Wps回调地址
  71. */
  72. function mapWpsRoutes(){
  73. foreach ( glob(base_path('routes'.DIRECTORY_SEPARATOR.'wps'.DIRECTORY_SEPARATOR).'*.php' ) as $fileName ) {
  74. $fileName = basename( $fileName );
  75. Route::namespace( $this->namespace."\\Wps" )
  76. ->prefix('wps')
  77. ->group( base_path('routes'.DIRECTORY_SEPARATOR.'wps'.DIRECTORY_SEPARATOR."{$fileName}") );
  78. }
  79. }
  80. /**
  81. * 总后台API路由
  82. */
  83. protected function mapAdminApiRoutes(){
  84. foreach ( glob(base_path('routes'.DIRECTORY_SEPARATOR.'adminApi'.DIRECTORY_SEPARATOR).'*.php' ) as $fileName ) {
  85. $fileName = basename( $fileName );
  86. //中间件过滤加载
  87. Route::middleware('adminApiVerify')
  88. ->namespace( $this->namespace."\\AdminApi" )
  89. ->prefix('adminApi')
  90. ->group( base_path('routes'.DIRECTORY_SEPARATOR.'adminApi'.DIRECTORY_SEPARATOR."{$fileName}") );
  91. }
  92. }
  93. }