RouteServiceProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. protected $namespace = 'App\Http\Controllers';
  19. /**
  20. * The controller namespace for the application.
  21. *
  22. * When present, controller route declarations will automatically be prefixed with this namespace.
  23. *
  24. * @var string|null
  25. */
  26. // protected $namespace = 'App\\Http\\Controllers';
  27. /**
  28. * Define your route model bindings, pattern filters, etc.
  29. *
  30. * @return void
  31. */
  32. public function boot()
  33. {
  34. // $this->configureRateLimiting();
  35. //后台接口路由
  36. $this->mapAdminApiRoutes();
  37. //前台接口路由
  38. $this->mapApiRoutes();
  39. $this->routes(function () {
  40. Route::middleware('web')
  41. ->namespace($this->namespace)
  42. ->group(base_path('routes/web.php'));
  43. });
  44. }
  45. /**
  46. * Configure the rate limiters for the application.
  47. *
  48. * @return void
  49. */
  50. protected function configureRateLimiting()
  51. {
  52. RateLimiter::for('api', function (Request $request) {
  53. return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
  54. });
  55. }
  56. /**
  57. * 总后台API路由
  58. */
  59. protected function mapAdminApiRoutes(){
  60. foreach ( glob(base_path('routes'.DIRECTORY_SEPARATOR.'adminApi'.DIRECTORY_SEPARATOR).'*.php' ) as $fileName ) {
  61. $fileName = basename( $fileName );
  62. $prefix = substr( $fileName,0,strrpos($fileName,'.') );
  63. $nameSpace = ucfirst($prefix);
  64. //中间件过滤加载
  65. Route::middleware('adminApi_verify')
  66. ->namespace( $this->namespace."\\AdminApi" )
  67. ->group( base_path('routes'.DIRECTORY_SEPARATOR.'adminApi'.DIRECTORY_SEPARATOR."{$fileName}") );
  68. }
  69. }
  70. /**
  71. * Define the "api" routes for the application.
  72. *
  73. * These routes are typically stateless.
  74. *
  75. * @return void
  76. */
  77. protected function mapApiRoutes()
  78. {
  79. Route::prefix('api')
  80. ->middleware('api')
  81. ->namespace($this->namespace)
  82. ->group(base_path('routes/api.php'));
  83. foreach ( glob(base_path('routes'.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR).'*.php' ) as $fileName ) {
  84. $fileName = basename( $fileName );
  85. Route::middleware('api_verify')
  86. ->namespace( $this->namespace."\\Api" )
  87. ->prefix('api')
  88. ->group( base_path('routes'.DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR."{$fileName}") );
  89. }
  90. }
  91. }