Skip to content

Commit df37653

Browse files
committed
route prefixes
1 parent ba31ab5 commit df37653

File tree

13 files changed

+61
-34
lines changed

13 files changed

+61
-34
lines changed

resources/views/app.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<meta name="turbo-prefetch" content="false">
89
<meta name="csrf-token" content="{{ csrf_token() }}">
910

1011
{{-- Styles --}}

routes/web.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use Cone\Root\Http\Controllers\DashboardController;
44
use Cone\Root\Http\Controllers\DownloadController;
55
use Cone\Root\Http\Controllers\ResourceController;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Support\Facades\Redirect;
68
use Illuminate\Support\Facades\Route;
79

810
// Dashboard
@@ -12,11 +14,14 @@
1214
Route::get('/download/{medium:uuid}', DownloadController::class)->name('download');
1315

1416
// Resource
15-
Route::get('/{resource}', [ResourceController::class, 'index'])->name('resource.index');
16-
Route::get('/{resource}/create', [ResourceController::class, 'create'])->name('resource.create');
17-
Route::post('/{resource}', [ResourceController::class, 'store'])->name('resource.store');
18-
Route::get('/{resource}/{resourceModel}', [ResourceController::class, 'show'])->name('resource.show');
19-
Route::get('/{resource}/{resourceModel}/edit', [ResourceController::class, 'edit'])->name('resource.edit');
20-
Route::patch('/{resource}/{resourceModel}', [ResourceController::class, 'update'])->name('resource.update');
21-
Route::delete('/{resource}/{resourceModel}', [ResourceController::class, 'destroy'])->name('resource.delete');
22-
Route::post('/{resource}/{resourceModel}/restore', [ResourceController::class, 'restore'])->name('resource.restore');
17+
Route::get('/resources', static function (): RedirectResponse {
18+
return Redirect::route('root.dashboard');
19+
});
20+
Route::get('/resources/{resource}', [ResourceController::class, 'index'])->name('resource.index');
21+
Route::get('/resources/{resource}/create', [ResourceController::class, 'create'])->name('resource.create');
22+
Route::post('/resources/{resource}', [ResourceController::class, 'store'])->name('resource.store');
23+
Route::get('/resources/{resource}/{resourceModel}', [ResourceController::class, 'show'])->name('resource.show');
24+
Route::get('/resources/{resource}/{resourceModel}/edit', [ResourceController::class, 'edit'])->name('resource.edit');
25+
Route::patch('/resources/{resource}/{resourceModel}', [ResourceController::class, 'update'])->name('resource.update');
26+
Route::delete('/resources/{resource}/{resourceModel}', [ResourceController::class, 'destroy'])->name('resource.delete');
27+
Route::post('/resources/{resource}/{resourceModel}/restore', [ResourceController::class, 'restore'])->name('resource.restore');

src/Resources/Resource.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ public function getUriKey(): string
126126
return $this->getKey();
127127
}
128128

129+
/**
130+
* Get the route prefix.
131+
*/
132+
public function getRoutePrefix(): string
133+
{
134+
return sprintf('resources/%s', $this->getUriKey());
135+
}
136+
129137
/**
130138
* Get the route parameter name.
131139
*/
@@ -572,7 +580,7 @@ public function registerRoutes(Request $request, Router $router): void
572580
$this->__registerRoutes($request, $router);
573581

574582
$router->group([
575-
'prefix' => $this->getUriKey(),
583+
'prefix' => $this->getRoutePrefix(),
576584
'middleware' => $this->getRouteMiddleware(),
577585
], function (Router $router) use ($request): void {
578586
$this->resolveActions($request)->registerRoutes($request, $router);

src/Root.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use DateTimeZone;
1313
use Illuminate\Contracts\Foundation\Application;
1414
use Illuminate\Http\Request;
15+
use Illuminate\Routing\Router;
1516
use Illuminate\Support\Facades\App;
1617
use Illuminate\Support\Facades\Config;
1718
use Illuminate\Support\Facades\Route;
@@ -98,6 +99,10 @@ public static function instance(): static
9899
*/
99100
public function boot(): void
100101
{
102+
$this->routes(function (Router $router): void {
103+
$this->widgets->registerRoutes($this->app['request'], $router);
104+
});
105+
101106
$this->resources->discoverIn($this->app->path('Root/Resources'));
102107

103108
$this->resources->each->boot($this);
@@ -108,14 +113,14 @@ public function boot(): void
108113

109114
$this->breadcrumbs->patterns([
110115
$this->getPath() => __('Dashboard'),
111-
sprintf('%s/{resource}', $this->getPath()) => static function (Request $request): string {
116+
sprintf('%s/resources/{resource}', $this->getPath()) => static function (Request $request): string {
112117
return $request->route('_resource')->getName();
113118
},
114-
sprintf('%s/{resource}/create', $this->getPath()) => __('Create'),
115-
sprintf('%s/{resource}/{resourceModel}', $this->getPath()) => static function (Request $request): string {
119+
sprintf('%s/resources/{resource}/create', $this->getPath()) => __('Create'),
120+
sprintf('%s/resources/{resource}/{resourceModel}', $this->getPath()) => static function (Request $request): string {
116121
return $request->route('_resource')->modelTitle($request->route('resourceModel'));
117122
},
118-
sprintf('%s/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'),
123+
sprintf('%s/resources/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'),
119124
]);
120125
}
121126

src/Traits/RegistersRoutes.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public function getUri(): ?string
2929
return $this->uri;
3030
}
3131

32+
/**
33+
* Get the route prefix.
34+
*/
35+
public function getRoutePrefix(): string
36+
{
37+
return (string) $this->getUriKey();
38+
}
39+
3240
/**
3341
* Get the route parameter name.
3442
*/
@@ -50,11 +58,11 @@ public function getRouteMiddleware(): array
5058
*/
5159
public function registerRoutes(Request $request, Router $router): void
5260
{
53-
$this->uri = Str::start(sprintf('%s/%s', $router->getLastGroupPrefix(), $this->getUriKey()), '/');
61+
$this->uri = Str::start(sprintf('%s/%s', $router->getLastGroupPrefix(), $this->getRoutePrefix()), '/');
5462

5563
if (! App::routesAreCached()) {
5664
$router->group([
57-
'prefix' => $this->getUriKey(),
65+
'prefix' => $this->getRoutePrefix(),
5866
'middleware' => $this->getRouteMiddleware(),
5967
], function (Router $router): void {
6068
$this->routes($router);

tests/Http/ActionControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function setUp(): void
1919
public function test_action_controller_handles_action_request(): void
2020
{
2121
$this->actingAs($this->admin)
22-
->post('/root/users/actions/send-password-reset-notification')
22+
->post('/root/resources/users/actions/send-password-reset-notification')
2323
->assertRedirect()
2424
->assertSessionHas('alerts.action-send-password-reset-notification');
2525
}

tests/Http/BelongsToManyControllerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function setUp(): void
4040
public function test_belongs_to_many_controller_handles_index(): void
4141
{
4242
$this->actingAs($this->admin)
43-
->get('/root/users/'.$this->admin->getKey().'/fields/teams')
43+
->get('/root/resources/users/'.$this->admin->getKey().'/fields/teams')
4444
->assertOk()
4545
->assertViewIs('root::resources.index')
4646
->assertViewHas($this->field->toIndex($this->app['request'], $this->admin));
@@ -51,7 +51,7 @@ public function test_belongs_to_many_controller_handles_store(): void
5151
$team = Team::factory()->create();
5252

5353
$this->actingAs($this->admin)
54-
->post('/root/users/'.$this->admin->getKey().'/fields/teams', [
54+
->post('/root/resources/users/'.$this->admin->getKey().'/fields/teams', [
5555
'related' => $team->getKey(),
5656
'role' => 'member',
5757
])
@@ -72,7 +72,7 @@ public function test_belongs_to_many_controller_handles_update(): void
7272
$this->assertSame('admin', $team->pivot->role);
7373

7474
$this->actingAs($this->admin)
75-
->patch('/root/users/'.$this->admin->getKey().'/fields/teams/'.$team->pivot->getKey(), [
75+
->patch('/root/resources/users/'.$this->admin->getKey().'/fields/teams/'.$team->pivot->getKey(), [
7676
'related' => $team->getKey(),
7777
'role' => 'member',
7878
])
@@ -87,7 +87,7 @@ public function test_belongs_to_many_controller_handles_destroy(): void
8787
$team = $this->admin->teams->first();
8888

8989
$this->actingAs($this->admin)
90-
->delete('/root/users/'.$this->admin->getKey().'/fields/teams/'.$team->pivot->getKey())
90+
->delete('/root/resources/users/'.$this->admin->getKey().'/fields/teams/'.$team->pivot->getKey())
9191
->assertRedirect()
9292
->assertSessionHas('alerts.relation-deleted');
9393

tests/Http/MediaControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setUp(): void
3636
public function test_media_controller_handles_index(): void
3737
{
3838
$this->actingAs($this->admin)
39-
->get('/root/users/'.$this->admin->getKey().'/fields/media')
39+
->get('/root/resources/users/'.$this->admin->getKey().'/fields/media')
4040
->assertOk()
4141
->assertJson($this->field->paginateRelatable($this->app['request'], $this->admin)->toArray());
4242
}
@@ -47,7 +47,7 @@ public function test_media_controller_handles_store(): void
4747

4848
$this->actingAs($this->admin)
4949
->post(
50-
'/root/users/'.$this->admin->getKey().'/fields/media',
50+
'/root/resources/users/'.$this->admin->getKey().'/fields/media',
5151
['file' => UploadedFile::fake()->image('test.png')],
5252
['X-Chunk-Index' => 1, 'X-Chunk-Total' => 1]
5353
)
@@ -64,7 +64,7 @@ public function test_media_controller_handles_destroy(): void
6464
$medium = Medium::factory()->create();
6565

6666
$this->actingAs($this->admin)
67-
->delete('/root/users/'.$this->admin->getKey().'/fields/media', ['ids' => [$medium->getKey()]])
67+
->delete('/root/resources/users/'.$this->admin->getKey().'/fields/media', ['ids' => [$medium->getKey()]])
6868
->assertOk()
6969
->assertJson(['deleted' => [1]]);
7070

tests/Http/MorphToControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function setUp(): void
3131
public function test_morph_to_controller_handles_request(): void
3232
{
3333
$this->actingAs($this->admin)
34-
->get('/root/users/'.$this->admin->getKey().'/fields/employer')
34+
->get('/root/resources/users/'.$this->admin->getKey().'/fields/employer')
3535
->assertOk()
3636
->assertViewIs('root::fields.morph-to')
3737
->assertViewHas($this->field->toInput($this->app['request'], $this->admin));

tests/Http/RelationControllerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function setUp(): void
3838
public function test_relation_controller_handles_index(): void
3939
{
4040
$this->actingAs($this->admin)
41-
->get('/root/users/'.$this->admin->getKey().'/fields/uploads')
41+
->get('/root/resources/users/'.$this->admin->getKey().'/fields/uploads')
4242
->assertOk()
4343
->assertViewIs('root::resources.index')
4444
->assertViewHas($this->field->toIndex($this->app['request'], $this->admin));
@@ -47,7 +47,7 @@ public function test_relation_controller_handles_index(): void
4747
public function test_relation_controller_handles_create(): void
4848
{
4949
$this->actingAs($this->admin)
50-
->get('/root/users/'.$this->admin->getKey().'/fields/uploads/create')
50+
->get('/root/resources/users/'.$this->admin->getKey().'/fields/uploads/create')
5151
->assertOk()
5252
->assertViewIs('root::resources.form')
5353
->assertViewHas($this->field->toCreate($this->app['request'], $this->admin));
@@ -56,7 +56,7 @@ public function test_relation_controller_handles_create(): void
5656
public function test_relation_controller_handles_store(): void
5757
{
5858
$this->actingAs($this->admin)
59-
->post('/root/users/'.$this->admin->getKey().'/fields/uploads', $data = Medium::factory()->make()->toArray())
59+
->post('/root/resources/users/'.$this->admin->getKey().'/fields/uploads', $data = Medium::factory()->make()->toArray())
6060
->assertRedirect()
6161
->assertSessionHas('alerts.relation-created');
6262

@@ -72,7 +72,7 @@ public function test_relation_controller_handles_show(): void
7272
});
7373

7474
$this->actingAs($this->admin)
75-
->get('/root/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey())
75+
->get('/root/resources/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey())
7676
->assertOk()
7777
->assertViewIs('root::resources.show')
7878
->assertViewHas($this->field->toShow($this->app['request'], $this->admin, $this->medium));
@@ -85,7 +85,7 @@ public function test_relation_controller_handles_edit(): void
8585
});
8686

8787
$this->actingAs($this->admin)
88-
->get('/root/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey().'/edit')
88+
->get('/root/resources/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey().'/edit')
8989
->assertOk()
9090
->assertViewIs('root::resources.form')
9191
->assertViewHas($this->field->toEdit($this->app['request'], $this->admin, $this->medium));
@@ -99,7 +99,7 @@ public function test_relation_controller_handles_update(): void
9999

100100
$this->actingAs($this->admin)
101101
->patch(
102-
'/root/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey(),
102+
'/root/resources/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey(),
103103
array_merge($this->medium->toArray(), ['file_name' => 'updated.png'])
104104
)
105105
->assertRedirect()
@@ -115,7 +115,7 @@ public function test_relation_controller_handles_destroy(): void
115115
});
116116

117117
$this->actingAs($this->admin)
118-
->delete('/root/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey())
118+
->delete('/root/resources/users/'.$this->admin->getKey().'/fields/uploads/'.$this->medium->getKey())
119119
->assertRedirect()
120120
->assertSessionHas('alerts.relation-deleted');
121121

0 commit comments

Comments
 (0)