AppServiceProvider.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\ServiceProvider;
  6. use Dcat\Admin\Layout\Content;
  7. class AppServiceProvider extends ServiceProvider
  8. {
  9. /**
  10. * Register any application services.
  11. *
  12. * @return void
  13. */
  14. public function register()
  15. {
  16. //添加自定义辅助函数
  17. require_once __DIR__.'/../helpers.php';
  18. //自定义内容
  19. $newContent = new Content();
  20. $newContent->view('admin.layouts.content');
  21. $this->app->singleton(Content::class, function () use ($newContent) {
  22. return $newContent;
  23. });
  24. }
  25. /**
  26. * Bootstrap any application services.
  27. *
  28. * @return void
  29. */
  30. public function boot()
  31. {
  32. //把SQL输出到日志中
  33. if ($this->app->environment('local')) { // 仅在本地环境启用
  34. DB::listen(function ($query) {
  35. // 格式化 SQL 语句
  36. $sql = $query->sql;
  37. foreach ($query->bindings as $binding) {
  38. $sql = preg_replace('/\?/', "'" . addslashes($binding) . "'", $sql, 1);
  39. }
  40. // 输出到控制台
  41. //dump($sql . ' [' . $query->time . 'ms]');
  42. //输出到log
  43. Log::info($sql . ' [' . $query->time . 'ms]');
  44. });
  45. }
  46. }
  47. }