-
Notifications
You must be signed in to change notification settings - Fork 311
/
RouteServiceProvider.php
158 lines (139 loc) · 4.52 KB
/
RouteServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace App\Providers;
use App\Http\Middleware\CheckMigrationStatus;
use App\Services\ModulesService;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* The path to the "home" route for your application.
*
* @var string
*/
public const HOME = '/';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
if ( request()->header( 'x-forwarded-proto' ) === 'https' ) {
URL::forceScheme( 'https' );
}
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapModulesRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware( 'web' )
->namespace( $this->namespace )
->group( base_path( 'routes/web.php' ) );
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix( 'api' )
->middleware( 'api' )
->namespace( $this->namespace )
->group( base_path( 'routes/api.php' ) );
}
/**
* Map module web defined route
*
* follow the same rules applied to self::mapWebRoutes();
*
* @return void
*/
protected function mapModulesRoutes()
{
/**
* @var ModulesService $Modules
*/
$modulesService = app()->make( ModulesService::class );
foreach ( $modulesService->getEnabledAndAutoloadedModules() as $module ) {
$domain = pathinfo( env( 'APP_URL' ) );
/**
* will load all web.php file as dashboard routes.
*/
if ( $module[ 'routes-file' ] !== false ) {
if ( env( 'NS_WILDCARD_ENABLED' ) ) {
/**
* The defined route should only be applicable
* to the main domain.
*/
$domainString = ( $domain[ 'filename' ] ?: 'localhost' ) . ( isset( $domain[ 'extension' ] ) ? '.' . $domain[ 'extension' ] : '' );
Route::domain( $domainString )->group( function () use ( $module ) {
$this->mapModuleWebRoutes( $module );
} );
} else {
$this->mapModuleWebRoutes( $module );
}
}
/**
* will load api.php file has api file
*/
if ( $module[ 'api-file' ] !== false ) {
if ( env( 'NS_WILDCARD_ENABLED' ) ) {
/**
* The defined route should only be applicable
* to the main domain.
*/
$domainString = ( $domain[ 'filename' ] ?: 'localhost' ) . ( isset( $domain[ 'extension' ] ) ? '.' . $domain[ 'extension' ] : '' );
Route::domain( $domainString )->group( function () use ( $module ) {
$this->mapModuleApiRoutes( $module );
} );
} else {
$this->mapModuleApiRoutes( $module );
}
}
}
}
public function mapModuleWebRoutes( $module )
{
Route::middleware( [ 'web', 'ns.installed', 'ns.check-application-health', CheckMigrationStatus::class ] )
->namespace( 'Modules\\' . $module[ 'namespace' ] . '\Http\Controllers' )
->group( $module[ 'routes-file' ] );
}
public function mapModuleApiRoutes( $module )
{
Route::prefix( 'api/' )
->middleware( [ 'ns.installed', 'api' ] )
->namespace( 'Modules\\' . $module[ 'namespace' ] . '\Http\Controllers' )
->group( $module[ 'api-file' ] );
}
}