Skip to content

Commit

Permalink
update routes
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Aug 16, 2022
1 parent fd77965 commit 13d7ee4
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function getKey(): string
*/
public function getUri(): string
{
return trim(sprintf('%s/%s', Root::getPath(), $this->getKey()), '/');
return Str::start(sprintf('%s/%s', Root::getPath(), $this->getKey()), '/');
}

/**
Expand Down Expand Up @@ -404,8 +404,8 @@ public function deleted(ResourceRequest $request, Model $model): void
public function toBreadcrumbs(): Breadcrumbs
{
return new Breadcrumbs([
sprintf('/%s', Root::getPath()) => __('Dashboard'),
sprintf('/%s', $this->getUri()) => $this->getName(),
Root::getPath() => __('Dashboard'),
$this->getUri() => $this->getName(),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

abstract class Root
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public static function routes(Closure $callback): void
*/
public static function getPath(): string
{
return (string) Config::get('root.path', 'root');
return Str::start(Config::get('root.path', 'root'), '/');
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/RegistersRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;

trait RegistersRoutes
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public function getUri(): ?string
*/
public function registerRoutes(RootRequest $request, Router $router): void
{
$this->setUri(sprintf('%s/%s', $router->getLastGroupPrefix(), $this->getKey()));
$this->setUri(sprintf('/%s/%s', $router->getLastGroupPrefix(), $this->getKey()));

if (! App::routesAreCached()) {
$router->prefix($this->getKey())
Expand All @@ -65,7 +66,7 @@ public function registerRoutes(RootRequest $request, Router $router): void
}

$router->matched(function (RouteMatched $event): void {
if (str_starts_with($event->route->uri(), $this->getUri())) {
if (str_starts_with(Str::start($event->route->uri(), '/'), $this->getUri())) {
$event->route->setParameter('resolved', $this);
}
});
Expand Down
6 changes: 3 additions & 3 deletions tests/Actions/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public function an_action_registers_routes()
$this->action->registerRoutes($this->request, $router);
});

$this->assertSame('posts/actions/publish-posts', $this->action->getUri());
$this->assertSame('/posts/actions/publish-posts', $this->action->getUri());

$this->assertArrayHasKey(
$this->action->getUri(),
trim($this->action->getUri(), '/'),
$this->app['router']->getRoutes()->get('POST')
);
}
Expand All @@ -116,7 +116,7 @@ public function an_action_has_array_representation()
'destructive' => $this->action->isDestructive(),
'key' => $this->action->getKey(),
'name' => $this->action->getName(),
'url' => $this->app['url']->to($this->action->getUri()),
'url' => $this->action->getUri(),
], $this->action->toArray());
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Extracts/ExtractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public function an_extract_registers_routes()
$this->extract->registerRoutes($this->request, $router);
});

$this->assertSame('posts/extracts/long-posts', $this->extract->getUri());
$this->assertSame('/posts/extracts/long-posts', $this->extract->getUri());

$this->assertArrayHasKey(
$this->extract->getUri(),
trim($this->extract->getUri(), '/'),
$this->app['router']->getRoutes()->get('GET')
);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public function an_extract_has_array_representation()
$this->assertSame([
'key' => $this->extract->getKey(),
'name' => $this->extract->getName(),
'url' => $this->app['url']->to($this->extract->getUri()),
'url' => $this->extract->getUri(),
], $this->extract->toArray());
}
}
4 changes: 2 additions & 2 deletions tests/Fields/EditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public function an_editor_field_register_routes()
$this->field->registerRoutes($this->request, $router);
});

$this->assertSame('posts/fields/content', $this->field->getUri());
$this->assertSame('/posts/fields/content', $this->field->getUri());

$this->assertArrayHasKey(
$this->field->getMedia()->getUri(),
trim($this->field->getMedia()->getUri(), '/'),
$this->app['router']->getRoutes()->get('GET')
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fields/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function a_json_field_registers_routes()
$this->field->registerRoutes($this->request, $router);
});

$this->assertSame('posts/fields/inventory', $this->field->getUri());
$this->assertSame('/posts/fields/inventory', $this->field->getUri());
}

/** @test */
Expand Down
4 changes: 2 additions & 2 deletions tests/Fields/RelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public function a_relation_field_registers_routes()
$this->field->registerRoutes($this->request, $router);
});

$this->assertSame('posts/fields/author', $this->field->getUri());
$this->assertSame('/posts/fields/author', $this->field->getUri());

$this->assertArrayHasKey(
$this->field->getUri(),
trim($this->field->getUri(), '/'),
$this->app['router']->getRoutes()->get('GET')
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Widgets/WidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public function an_async_widget_registers_routes()
$this->widget->registerRoutes($this->request, $router);
});

$this->assertSame('posts/widgets/posts-count', $this->widget->getUri());
$this->assertSame('/posts/widgets/posts-count', $this->widget->getUri());

$this->assertArrayHasKey(
$this->widget->getUri(),
trim($this->widget->getUri(), '/'),
$this->app['router']->getRoutes()->get('GET')
);
}
Expand Down

0 comments on commit 13d7ee4

Please sign in to comment.