-
Notifications
You must be signed in to change notification settings - Fork 554
/
RouteServiceProvider.php
165 lines (137 loc) · 6.95 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
159
160
161
162
163
164
165
<?php
namespace A17\Twill;
use A17\Twill\Http\Middleware\Impersonate;
use A17\Twill\Http\Middleware\NoDebugBar;
use A17\Twill\Http\Middleware\RedirectIfAuthenticated;
use A17\Twill\Http\Middleware\ValidateBackHistory;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use Route;
class RouteServiceProvider extends ServiceProvider
{
protected $namespace = 'A17\Twill\Http\Controllers';
public function boot()
{
$this->registerRouteMiddlewares();
$this->registerMacros();
parent::boot();
}
public function map(Router $router)
{
$router->group([
'namespace' => config('twill.namespace', 'App') . '\Http\Controllers\Admin',
'domain' => config('twill.admin_app_url'),
'as' => 'admin.',
'middleware' => [config('twill.admin_middleware_group', 'web')],
'prefix' => rtrim(ltrim(config('twill.admin_app_path'), '/'), '/'),
], function ($router) {
$router->group(['middleware' => ['auth', 'impersonate', 'validateBackHistory']], function ($router) {
require base_path('routes/admin.php');
});
});
$router->group([
'namespace' => $this->namespace . '\Admin',
'domain' => config('twill.admin_app_url'),
'as' => 'admin.',
'middleware' => [config('twill.admin_middleware_group', 'web')],
'prefix' => rtrim(ltrim(config('twill.admin_app_path'), '/'), '/'),
],
function ($router) {
$router->group(['middleware' => ['auth', 'impersonate', 'validateBackHistory']], function ($router) {
require __DIR__ . '/../routes/admin.php';
});
$router->group(['middleware' => ['noDebugBar']], function ($router) {
require __DIR__ . '/../routes/auth.php';
});
$router->group(['middleware' => array_merge(['noDebugBar'], (app()->environment('production') ? ['auth'] : []))], function ($router) {
require __DIR__ . '/../routes/templates.php';
});
}
);
if (config('twill.templates_on_frontend_domain')) {
$router->group([
'namespace' => $this->namespace . '\Admin',
'domain' => config('app.url'),
'middleware' => [config('twill.admin_middleware_group', 'web')],
],
function ($router) {
$router->group(['middleware' => array_merge(['noDebugBar'], (app()->environment('production') ? ['auth'] : []))], function ($router) {
require __DIR__ . '/../routes/templates.php';
});
}
);
}
}
private function registerRouteMiddlewares()
{
/*
* See Laravel 5.4 Changelog https://laravel.com/docs/5.4/upgrade
* The middleware method of the Illuminate\Routing\Router class has been renamed to aliasMiddleware().
*/
$middlewareRegisterMethod = method_exists(app('router'), 'aliasMiddleware') ? 'aliasMiddleware' : 'middleware';
Route::$middlewareRegisterMethod('noDebugBar', NoDebugBar::class);
Route::$middlewareRegisterMethod('impersonate', Impersonate::class);
Route::$middlewareRegisterMethod('guest', RedirectIfAuthenticated::class);
Route::$middlewareRegisterMethod('validateBackHistory', ValidateBackHistory::class);
}
protected function registerMacros()
{
Route::macro('moduleShowWithPreview', function ($moduleName, $routePrefix = null, $controllerName = null) {
if ($routePrefix === null) {
$routePrefix = $moduleName;
}
if ($controllerName === null) {
$controllerName = ucfirst(str_plural($moduleName));
}
$routePrefix = empty($routePrefix) ? '/' : (starts_with($routePrefix, '/') ? $routePrefix : '/' . $routePrefix);
$routePrefix = ends_with($routePrefix, '/') ? $routePrefix : $routePrefix . '/';
Route::name($moduleName . '.show')->get($routePrefix . '{slug}', $controllerName . 'Controller@show');
Route::name($moduleName . '.preview')->get('/admin-preview' . $routePrefix . '{slug}', $controllerName . 'Controller@show')->middleware(['web', 'auth', 'can:list']);
});
Route::macro('module', function ($slug, $options = [], $resource_options = [], $resource = true) {
$slugs = explode('.', $slug);
$prefixSlug = str_replace('.', "/", $slug);
$_slug = Arr::last($slugs);
$className = implode("", array_map(function ($s) {
return ucfirst(str_singular($s));
}, $slugs));
$customRoutes = $defaults = ['reorder', 'publish', 'bulkPublish', 'browser', 'feature', 'bulkFeature', 'tags', 'preview', 'restore', 'bulkRestore', 'status', 'bulkDelete', 'restoreRevision'];
if (isset($options['only'])) {
$customRoutes = array_intersect($defaults, (array) $options['only']);
} elseif (isset($options['except'])) {
$customRoutes = array_diff($defaults, (array) $options['except']);
}
$groupPrefix = trim(str_replace('/', '.', Route::getLastGroupPrefix()), '.');
if (!empty(config('twill.admin_app_path'))) {
$groupPrefix = ltrim(str_replace(config('twill.admin_app_path'), '', $groupPrefix), '.');
}
$customRoutePrefix = !empty($groupPrefix) ? "{$groupPrefix}.{$slug}" : "{$slug}";
foreach ($customRoutes as $route) {
$routeSlug = "{$prefixSlug}/{$route}";
$mapping = ['as' => $customRoutePrefix . ".{$route}", 'uses' => "{$className}Controller@{$route}"];
if (in_array($route, ['browser', 'tags'])) {
Route::get($routeSlug, $mapping);
}
if (in_array($route, ['status'])) {
Route::get($routeSlug . "/{id}", $mapping);
}
if (in_array($route, ['publish', 'feature', 'restore', 'restoreRevision'])) {
Route::put($routeSlug, $mapping);
}
if (in_array($route, ['preview'])) {
Route::put($routeSlug . "/{id}", $mapping);
}
if (in_array($route, ['reorder', 'bulkPublish', 'bulkFeature', 'bulkDelete', 'bulkRestore'])) {
Route::post($routeSlug, $mapping);
}
}
if ($resource) {
$customRoutePrefix = !empty($groupPrefix) ? "{$groupPrefix}." : "";
Route::group(['as' => $customRoutePrefix], function () use ($slug, $className, $resource_options) {
Route::resource($slug, "{$className}Controller", $resource_options);
});
}
});
}
}