Skip to content

Commit

Permalink
route prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 19, 2024
1 parent ba31ab5 commit df37653
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 34 deletions.
1 change: 1 addition & 0 deletions resources/views/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="turbo-prefetch" content="false">
<meta name="csrf-token" content="{{ csrf_token() }}">

{{-- Styles --}}
Expand Down
21 changes: 13 additions & 8 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Cone\Root\Http\Controllers\DashboardController;
use Cone\Root\Http\Controllers\DownloadController;
use Cone\Root\Http\Controllers\ResourceController;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;

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

// Resource
Route::get('/{resource}', [ResourceController::class, 'index'])->name('resource.index');
Route::get('/{resource}/create', [ResourceController::class, 'create'])->name('resource.create');
Route::post('/{resource}', [ResourceController::class, 'store'])->name('resource.store');
Route::get('/{resource}/{resourceModel}', [ResourceController::class, 'show'])->name('resource.show');
Route::get('/{resource}/{resourceModel}/edit', [ResourceController::class, 'edit'])->name('resource.edit');
Route::patch('/{resource}/{resourceModel}', [ResourceController::class, 'update'])->name('resource.update');
Route::delete('/{resource}/{resourceModel}', [ResourceController::class, 'destroy'])->name('resource.delete');
Route::post('/{resource}/{resourceModel}/restore', [ResourceController::class, 'restore'])->name('resource.restore');
Route::get('/resources', static function (): RedirectResponse {
return Redirect::route('root.dashboard');
});
Route::get('/resources/{resource}', [ResourceController::class, 'index'])->name('resource.index');
Route::get('/resources/{resource}/create', [ResourceController::class, 'create'])->name('resource.create');
Route::post('/resources/{resource}', [ResourceController::class, 'store'])->name('resource.store');
Route::get('/resources/{resource}/{resourceModel}', [ResourceController::class, 'show'])->name('resource.show');
Route::get('/resources/{resource}/{resourceModel}/edit', [ResourceController::class, 'edit'])->name('resource.edit');
Route::patch('/resources/{resource}/{resourceModel}', [ResourceController::class, 'update'])->name('resource.update');
Route::delete('/resources/{resource}/{resourceModel}', [ResourceController::class, 'destroy'])->name('resource.delete');
Route::post('/resources/{resource}/{resourceModel}/restore', [ResourceController::class, 'restore'])->name('resource.restore');
10 changes: 9 additions & 1 deletion src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public function getUriKey(): string
return $this->getKey();
}

/**
* Get the route prefix.
*/
public function getRoutePrefix(): string
{
return sprintf('resources/%s', $this->getUriKey());
}

/**
* Get the route parameter name.
*/
Expand Down Expand Up @@ -572,7 +580,7 @@ public function registerRoutes(Request $request, Router $router): void
$this->__registerRoutes($request, $router);

$router->group([
'prefix' => $this->getUriKey(),
'prefix' => $this->getRoutePrefix(),
'middleware' => $this->getRouteMiddleware(),
], function (Router $router) use ($request): void {
$this->resolveActions($request)->registerRoutes($request, $router);
Expand Down
13 changes: 9 additions & 4 deletions src/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DateTimeZone;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -98,6 +99,10 @@ public static function instance(): static
*/
public function boot(): void
{
$this->routes(function (Router $router): void {
$this->widgets->registerRoutes($this->app['request'], $router);
});

$this->resources->discoverIn($this->app->path('Root/Resources'));

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

$this->breadcrumbs->patterns([
$this->getPath() => __('Dashboard'),
sprintf('%s/{resource}', $this->getPath()) => static function (Request $request): string {
sprintf('%s/resources/{resource}', $this->getPath()) => static function (Request $request): string {
return $request->route('_resource')->getName();
},
sprintf('%s/{resource}/create', $this->getPath()) => __('Create'),
sprintf('%s/{resource}/{resourceModel}', $this->getPath()) => static function (Request $request): string {
sprintf('%s/resources/{resource}/create', $this->getPath()) => __('Create'),
sprintf('%s/resources/{resource}/{resourceModel}', $this->getPath()) => static function (Request $request): string {
return $request->route('_resource')->modelTitle($request->route('resourceModel'));
},
sprintf('%s/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'),
sprintf('%s/resources/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'),
]);
}

Expand Down
12 changes: 10 additions & 2 deletions src/Traits/RegistersRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function getUri(): ?string
return $this->uri;
}

/**
* Get the route prefix.
*/
public function getRoutePrefix(): string
{
return (string) $this->getUriKey();
}

/**
* Get the route parameter name.
*/
Expand All @@ -50,11 +58,11 @@ public function getRouteMiddleware(): array
*/
public function registerRoutes(Request $request, Router $router): void
{
$this->uri = Str::start(sprintf('%s/%s', $router->getLastGroupPrefix(), $this->getUriKey()), '/');
$this->uri = Str::start(sprintf('%s/%s', $router->getLastGroupPrefix(), $this->getRoutePrefix()), '/');

if (! App::routesAreCached()) {
$router->group([
'prefix' => $this->getUriKey(),
'prefix' => $this->getRoutePrefix(),
'middleware' => $this->getRouteMiddleware(),
], function (Router $router): void {
$this->routes($router);
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/ActionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp(): void
public function test_action_controller_handles_action_request(): void
{
$this->actingAs($this->admin)
->post('/root/users/actions/send-password-reset-notification')
->post('/root/resources/users/actions/send-password-reset-notification')
->assertRedirect()
->assertSessionHas('alerts.action-send-password-reset-notification');
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Http/BelongsToManyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function setUp(): void
public function test_belongs_to_many_controller_handles_index(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/teams')
->get('/root/resources/users/'.$this->admin->getKey().'/fields/teams')
->assertOk()
->assertViewIs('root::resources.index')
->assertViewHas($this->field->toIndex($this->app['request'], $this->admin));
Expand All @@ -51,7 +51,7 @@ public function test_belongs_to_many_controller_handles_store(): void
$team = Team::factory()->create();

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions tests/Http/MediaControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function setUp(): void
public function test_media_controller_handles_index(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/media')
->get('/root/resources/users/'.$this->admin->getKey().'/fields/media')
->assertOk()
->assertJson($this->field->paginateRelatable($this->app['request'], $this->admin)->toArray());
}
Expand All @@ -47,7 +47,7 @@ public function test_media_controller_handles_store(): void

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Http/MorphToControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void
public function test_morph_to_controller_handles_request(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/employer')
->get('/root/resources/users/'.$this->admin->getKey().'/fields/employer')
->assertOk()
->assertViewIs('root::fields.morph-to')
->assertViewHas($this->field->toInput($this->app['request'], $this->admin));
Expand Down
14 changes: 7 additions & 7 deletions tests/Http/RelationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function setUp(): void
public function test_relation_controller_handles_index(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/uploads')
->get('/root/resources/users/'.$this->admin->getKey().'/fields/uploads')
->assertOk()
->assertViewIs('root::resources.index')
->assertViewHas($this->field->toIndex($this->app['request'], $this->admin));
Expand All @@ -47,7 +47,7 @@ public function test_relation_controller_handles_index(): void
public function test_relation_controller_handles_create(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/uploads/create')
->get('/root/resources/users/'.$this->admin->getKey().'/fields/uploads/create')
->assertOk()
->assertViewIs('root::resources.form')
->assertViewHas($this->field->toCreate($this->app['request'], $this->admin));
Expand All @@ -56,7 +56,7 @@ public function test_relation_controller_handles_create(): void
public function test_relation_controller_handles_store(): void
{
$this->actingAs($this->admin)
->post('/root/users/'.$this->admin->getKey().'/fields/uploads', $data = Medium::factory()->make()->toArray())
->post('/root/resources/users/'.$this->admin->getKey().'/fields/uploads', $data = Medium::factory()->make()->toArray())
->assertRedirect()
->assertSessionHas('alerts.relation-created');

Expand All @@ -72,7 +72,7 @@ public function test_relation_controller_handles_show(): void
});

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Http/RepeaterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void
public function test_repeater_controller_handles_request(): void
{
$this->actingAs($this->admin)
->post('/root/users/'.$this->admin->getKey().'/fields/settings')
->post('/root/resources/users/'.$this->admin->getKey().'/fields/settings')
->assertOk();
}
}
2 changes: 1 addition & 1 deletion tests/Http/WidgetControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void
public function test_widget_controller_handles_request(): void
{
$this->actingAs($this->admin)
->get('/root/users/widgets/users-count')
->get('/root/resources/users/widgets/users-count')
->assertOk()
->assertViewIs($this->widget->getTemplate())
->assertViewHas($this->widget->data($this->app['request']));
Expand Down
2 changes: 1 addition & 1 deletion tests/Resources/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function test_a_resource_registers_routes(): void
{
$action = $this->resource->resolveActions($this->app['request'])->first();

$this->assertSame('/root/users/actions/send-password-reset-notification', $action->getUri());
$this->assertSame('/root/resources/users/actions/send-password-reset-notification', $action->getUri());

$this->assertArrayHasKey(
trim($action->getUri(), '/'),
Expand Down

0 comments on commit df37653

Please sign in to comment.