AdminServiceProvider.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Encore\Admin;
  3. use Illuminate\Support\Facades\Blade;
  4. use Illuminate\Support\ServiceProvider;
  5. class AdminServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * @var array
  9. */
  10. protected $commands = [
  11. Console\AdminCommand::class,
  12. Console\MakeCommand::class,
  13. Console\MenuCommand::class,
  14. Console\InstallCommand::class,
  15. Console\PublishCommand::class,
  16. Console\UninstallCommand::class,
  17. Console\ImportCommand::class,
  18. Console\CreateUserCommand::class,
  19. Console\ResetPasswordCommand::class,
  20. Console\ExtendCommand::class,
  21. Console\ExportSeedCommand::class,
  22. ];
  23. /**
  24. * The application's route middleware.
  25. *
  26. * @var array
  27. */
  28. protected $routeMiddleware = [
  29. 'admin.session' => Middleware\Session::class,
  30. 'admin.auth' => Middleware\Authenticate::class,
  31. 'admin.pjax' => Middleware\Pjax::class,
  32. 'admin.log' => Middleware\LogOperation::class,
  33. 'admin.permission' => Middleware\Permission::class,
  34. 'admin.bootstrap' => Middleware\Bootstrap::class,
  35. ];
  36. /**
  37. * The application's route middleware groups.
  38. *
  39. * @var array
  40. */
  41. protected $middlewareGroups = [
  42. 'admin' => [
  43. 'admin.session',
  44. 'admin.auth',
  45. 'admin.pjax',
  46. 'admin.log',
  47. 'admin.bootstrap',
  48. 'admin.permission',
  49. ],
  50. ];
  51. /**
  52. * Boot the service provider.
  53. *
  54. * @return void
  55. */
  56. public function boot()
  57. {
  58. $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');
  59. if (config('admin.https') || config('admin.secure')) {
  60. \URL::forceScheme('https');
  61. $this->app['request']->server->set('HTTPS', true);
  62. }
  63. if (file_exists($routes = admin_path('routes.php'))) {
  64. $this->loadRoutesFrom($routes);
  65. }
  66. if ($this->app->runningInConsole()) {
  67. $this->publishes([__DIR__.'/../config' => config_path()], 'laravel-admin-config');
  68. $this->publishes([__DIR__.'/../resources/lang' => resource_path('lang')], 'laravel-admin-lang');
  69. // $this->publishes([__DIR__.'/../resources/views' => resource_path('views/vendor/admin')], 'laravel-admin-views');
  70. $this->publishes([__DIR__.'/../database/migrations' => database_path('migrations')], 'laravel-admin-migrations');
  71. $this->publishes([__DIR__.'/../resources/assets' => public_path('vendor/laravel-admin')], 'laravel-admin-assets');
  72. }
  73. //remove default feature of double encoding enable in laravel 5.6 or later.
  74. $bladeReflectionClass = new \ReflectionClass('\Illuminate\View\Compilers\BladeCompiler');
  75. if ($bladeReflectionClass->hasMethod('withoutDoubleEncoding')) {
  76. Blade::withoutDoubleEncoding();
  77. }
  78. }
  79. /**
  80. * Register the service provider.
  81. *
  82. * @return void
  83. */
  84. public function register()
  85. {
  86. $this->loadAdminAuthConfig();
  87. $this->registerRouteMiddleware();
  88. $this->commands($this->commands);
  89. }
  90. /**
  91. * Setup auth configuration.
  92. *
  93. * @return void
  94. */
  95. protected function loadAdminAuthConfig()
  96. {
  97. config(array_dot(config('admin.auth', []), 'auth.'));
  98. }
  99. /**
  100. * Register the route middleware.
  101. *
  102. * @return void
  103. */
  104. protected function registerRouteMiddleware()
  105. {
  106. // register route middleware.
  107. foreach ($this->routeMiddleware as $key => $middleware) {
  108. app('router')->aliasMiddleware($key, $middleware);
  109. }
  110. // register middleware group.
  111. foreach ($this->middlewareGroups as $key => $middleware) {
  112. app('router')->middlewareGroup($key, $middleware);
  113. }
  114. }
  115. }