app.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. require_once __DIR__.'/../vendor/autoload.php';
  3. (new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
  4. dirname(__DIR__)
  5. ))->bootstrap();
  6. $site = require_once __DIR__.'/../config/site.php';
  7. date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
  8. /*
  9. |--------------------------------------------------------------------------
  10. | Create The Application
  11. |--------------------------------------------------------------------------
  12. |
  13. | Here we will load the environment and create the application instance
  14. | that serves as the central piece of this framework. We'll use this
  15. | application as an "IoC" container and router for this framework.
  16. |
  17. */
  18. $envFile = '.env';
  19. $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'] ?? '');
  20. if (!empty($site[$host])) {
  21. $envFile = $site[$host];
  22. }
  23. $subject = !empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ($_SERVER['REDIRECT_URL'] ?? '');
  24. if (!empty($subject)) {
  25. $pattern = '/\/(.*?)\//i';
  26. if (preg_match($pattern, $subject, $matches) && !empty($matches[1])) {
  27. if (!empty($site[$matches[1]])) {
  28. $envFile = $site[$matches[1]];
  29. }
  30. }
  31. }
  32. (new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
  33. dirname(__DIR__),
  34. $envFile
  35. ))->bootstrap();
  36. $app = new Laravel\Lumen\Application(
  37. dirname(__DIR__)
  38. );
  39. $app->withFacades();
  40. //注册mongodb 必须在withEloquent之前
  41. // $app->register(\Jenssegers\Mongodb\MongodbServiceProvider::class);
  42. $app->withEloquent();
  43. /*
  44. |--------------------------------------------------------------------------
  45. | Register Container Bindings
  46. |--------------------------------------------------------------------------
  47. |
  48. | Now we will register a few bindings in the service container. We will
  49. | register the exception handler and the console kernel. You may add
  50. | your own bindings here if you like or you can make another file.
  51. |
  52. */
  53. $app->singleton(
  54. Illuminate\Contracts\Debug\ExceptionHandler::class,
  55. App\Exceptions\Handler::class
  56. );
  57. $app->singleton(
  58. Illuminate\Contracts\Console\Kernel::class,
  59. App\Console\Kernel::class
  60. );
  61. /*
  62. |--------------------------------------------------------------------------
  63. | Register Config Files
  64. |--------------------------------------------------------------------------
  65. |
  66. | Now we will register the "app" configuration file. If the file exists in
  67. | your configuration directory it will be loaded; otherwise, we'll load
  68. | the default version. You may register other files below as needed.
  69. |
  70. */
  71. $app->configure('cache');
  72. $app->configure('app');
  73. $app->configure('auth');
  74. $app->configure('error');
  75. $app->configure('sms');
  76. $app->configure('oss');
  77. $app->configure('api');
  78. $app->configure('mail');
  79. $app->configure('services');
  80. $app->configure('wechat');
  81. /*
  82. |--------------------------------------------------------------------------
  83. | Register Middleware
  84. |--------------------------------------------------------------------------
  85. |
  86. | Next, we will register the middleware with the application. These can
  87. | be global middleware that run before and after each request into a
  88. | route or middleware that'll be assigned to some specific routes.
  89. |
  90. */
  91. $app->middleware([
  92. //全局添加返回值格式
  93. \App\Http\Middleware\Response::class
  94. // App\Http\Middleware\ExampleMiddleware::class
  95. ]);
  96. $app->routeMiddleware([
  97. 'admin.auth' => \App\Http\Middleware\AdminUserAuthenticate::class,
  98. 'normal.auth' => \App\Http\Middleware\NormalUserAuthenticate::class,
  99. ]);
  100. /*
  101. |--------------------------------------------------------------------------
  102. | Register Service Providers
  103. |--------------------------------------------------------------------------
  104. |
  105. | Here we will register all of the application's service providers which
  106. | are used to bind services into the container. Service providers are
  107. | totally optional, so you are not required to uncomment this line.
  108. |
  109. */
  110. // $app->register(App\Providers\AppServiceProvider::class);
  111. $app->register(Maatwebsite\Excel\ExcelServiceProvider::class);
  112. $app->alias('Excel', Maatwebsite\Excel\Facades\Excel::class);
  113. $app->register(App\Providers\AuthServiceProvider::class);
  114. $app->register(App\Providers\EventServiceProvider::class);
  115. $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
  116. $app->register(Illuminate\Mail\MailServiceProvider::class);
  117. $app->alias('mailer', Illuminate\Mail\Mailer::class);
  118. $app->alias('mailer', Illuminate\Contracts\Mail\Mailer::class);
  119. $app->alias('mailer', Illuminate\Contracts\Mail\MailQueue::class);
  120. $app->alias('Tymon\JWTAuth\Facades\JWTAuth', 'JWTAuth');
  121. $app->alias('Tymon\JWTAuth\Facades\JWTFactory', 'JWTFactory');
  122. /*
  123. |--------------------------------------------------------------------------
  124. | Load The Application Routes
  125. |--------------------------------------------------------------------------
  126. |
  127. | Next we will include the routes file so that they can all be added to
  128. | the application. This will provide all of the URLs the application
  129. | can respond to, as well as the controllers that may handle them.
  130. |
  131. */
  132. return $app;