|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modules\Sample\Providers; |
| 4 | + |
| 5 | +use App\Services\ModuleService; |
| 6 | +use Illuminate\Support\ServiceProvider; |
| 7 | +use Route; |
| 8 | + |
| 9 | +class SampleServiceProvider extends ServiceProvider |
| 10 | +{ |
| 11 | + protected $moduleSvc; |
| 12 | + |
| 13 | + /** |
| 14 | + * Boot the application events. |
| 15 | + */ |
| 16 | + public function boot() |
| 17 | + { |
| 18 | + $this->moduleSvc = app(ModuleService::class); |
| 19 | + |
| 20 | + $this->registerRoutes(); |
| 21 | + $this->registerTranslations(); |
| 22 | + $this->registerConfig(); |
| 23 | + $this->registerViews(); |
| 24 | + |
| 25 | + $this->registerLinks(); |
| 26 | + |
| 27 | + $this->loadMigrationsFrom(__DIR__.'/../Database/migrations'); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Register the service provider. |
| 32 | + */ |
| 33 | + public function register() |
| 34 | + { |
| 35 | + // |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Add module links here |
| 40 | + */ |
| 41 | + public function registerLinks() |
| 42 | + { |
| 43 | + // Show this link if logged in |
| 44 | + // $this->moduleSvc->addFrontendLink('Sample', '/sample', '', $logged_in=true); |
| 45 | + |
| 46 | + // Admin links: |
| 47 | + $this->moduleSvc->addAdminLink('Sample', '/admin/sample'); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Register the routes |
| 52 | + */ |
| 53 | + protected function registerRoutes() |
| 54 | + { |
| 55 | + /* |
| 56 | + * Routes for the frontend |
| 57 | + */ |
| 58 | + Route::group([ |
| 59 | + 'as' => 'sample.', |
| 60 | + 'prefix' => 'sample', |
| 61 | + // If you want a RESTful module, change this to 'api' |
| 62 | + 'middleware' => ['web'], |
| 63 | + 'namespace' => 'Modules\Sample\Http\Controllers', |
| 64 | + ], function () { |
| 65 | + $this->loadRoutesFrom(__DIR__.'/../Http/Routes/web.php'); |
| 66 | + }); |
| 67 | + |
| 68 | + /* |
| 69 | + * Routes for the admin |
| 70 | + */ |
| 71 | + Route::group([ |
| 72 | + 'as' => 'sample.', |
| 73 | + 'prefix' => 'admin/sample', |
| 74 | + // If you want a RESTful module, change this to 'api' |
| 75 | + 'middleware' => ['web', 'role:admin'], |
| 76 | + 'namespace' => 'Modules\Sample\Http\Controllers\Admin', |
| 77 | + ], function () { |
| 78 | + $this->loadRoutesFrom(__DIR__.'/../Http/Routes/admin.php'); |
| 79 | + }); |
| 80 | + |
| 81 | + /* |
| 82 | + * Routes for an API |
| 83 | + */ |
| 84 | + Route::group([ |
| 85 | + 'as' => 'sample.', |
| 86 | + 'prefix' => 'api/sample', |
| 87 | + // If you want a RESTful module, change this to 'api' |
| 88 | + 'middleware' => ['api'], |
| 89 | + 'namespace' => 'Modules\Sample\Http\Controllers\Api', |
| 90 | + ], function () { |
| 91 | + $this->loadRoutesFrom(__DIR__.'/../Http/Routes/api.php'); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Register config. |
| 97 | + */ |
| 98 | + protected function registerConfig() |
| 99 | + { |
| 100 | + $this->publishes([ |
| 101 | + __DIR__.'/../Config/config.php' => config_path('sample.php'), |
| 102 | + ], 'config'); |
| 103 | + |
| 104 | + $this->mergeConfigFrom( |
| 105 | + __DIR__.'/../Config/config.php', 'sample' |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Register views. |
| 111 | + */ |
| 112 | + public function registerViews() |
| 113 | + { |
| 114 | + $viewPath = resource_path('views/modules/sample'); |
| 115 | + $sourcePath = __DIR__.'/../Resources/views'; |
| 116 | + |
| 117 | + $this->publishes([ |
| 118 | + $sourcePath => $viewPath, |
| 119 | + ], 'views'); |
| 120 | + |
| 121 | + $paths = array_map( |
| 122 | + function ($path) { |
| 123 | + return $path.'/modules/sample'; |
| 124 | + }, |
| 125 | + \Config::get('view.paths') |
| 126 | + ); |
| 127 | + |
| 128 | + $paths[] = $sourcePath; |
| 129 | + $this->loadViewsFrom($paths, 'sample'); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Register translations. |
| 134 | + */ |
| 135 | + public function registerTranslations() |
| 136 | + { |
| 137 | + $langPath = resource_path('lang/modules/sample'); |
| 138 | + |
| 139 | + if (is_dir($langPath)) { |
| 140 | + $this->loadTranslationsFrom($langPath, 'sample'); |
| 141 | + } else { |
| 142 | + $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'sample'); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get the services provided by the provider. |
| 148 | + */ |
| 149 | + public function provides(): array |
| 150 | + { |
| 151 | + return []; |
| 152 | + } |
| 153 | +} |
0 commit comments