Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Routing Urls #47

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 6
level: 5
paths:
- src
- config
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/en/page-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
'url' => 'URL',
],

'errors' => [
'slug_starts_or_ends_with_slash' => 'The slug cannot start or end with a slash.',
],

'actions' => [
'visit' => 'Visit',
],
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Support\Facades\Route;
use Z3d0X\FilamentFabricator\Facades\FilamentFabricator;
use Z3d0X\FilamentFabricator\Http\Controllers\PageController;

if (config('filament-fabricator.routing.enabled')) {
Route::middleware(SubstituteBindings::class)
->prefix(config('filament-fabricator.routing.prefix', null))
->prefix(FilamentFabricator::getRoutingPrefix())
->group(function () {
Route::get('/{filamentFabricatorPage?}', PageController::class)
->where('filamentFabricatorPage', '.*')
Expand Down
1 change: 1 addition & 0 deletions src/Facades/FilamentFabricator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @method static array getStyles()
* @method static ?string getFavicon()
* @method static class-string<PageContract> getPageModel()
* @method static string getRoutingPrefix()
* @method static array getPageUrls()
* @method static ?string getPageUrlFromId(int $id, bool $prefixSlash = false)
*
Expand Down
20 changes: 18 additions & 2 deletions src/FilamentFabricatorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Z3d0X\FilamentFabricator\Layouts\Layout;
use Z3d0X\FilamentFabricator\Models\Contracts\Page as PageContract;
use Z3d0X\FilamentFabricator\Models\Page;
Expand Down Expand Up @@ -156,6 +157,17 @@ public function getPageModel(): string
return config('filament-fabricator.page-model') ?? Page::class;
}

public function getRoutingPrefix(): ?string
{
$prefix = config('filament-fabricator.routing.prefix');

if (is_null($prefix)) {
return null;
}

return Str::start(config('filament-fabricator.routing.prefix'), '/');
}

public function getPageUrls(): array
{
return Cache::rememberForever('filament-fabricator::page-urls', function () {
Expand All @@ -174,7 +186,11 @@ public function getPageUrlFromId(int $id, bool $prefixSlash = false): ?string
{
$url = $this->getPageUrls()[$id];

return ($url[0] !== '/' && $prefixSlash) ? "/{$url}" : $url;
if ($routingPrefix = $this->getRoutingPrefix()) {
$url = Str::start($url, $routingPrefix);
}

return $url;
}

protected function setPageUrl(PageContract $page, ?string $parentUrl = null): string
Expand All @@ -187,6 +203,6 @@ protected function setPageUrl(PageContract $page, ?string $parentUrl = null): st
}
}

return $this->pageUrls[$page->id] = $pageUrl;
return $this->pageUrls[$page->id] = Str::start($pageUrl, '/');
}
}
2 changes: 2 additions & 0 deletions src/FilamentFabricatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public function bootingPackage(): void

$pageUrls = FilamentFabricator::getPageUrls();

$value = Str::start($value, '/');

$pageId = array_search($value, $pageUrls);

return $pageModel::query()
Expand Down
15 changes: 11 additions & 4 deletions src/Resources/PageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function form(Form $form): Form
->schema([
Placeholder::make('page_url')
->visible(fn (?PageContract $record) => config('filament-fabricator.routing.enabled') && filled($record))
->content(fn (?PageContract $record) => config('filament-fabricator.routing.prefix') . FilamentFabricator::getPageUrlFromId($record?->id, true)),
->content(fn (?PageContract $record) => FilamentFabricator::getPageUrlFromId($record?->id)),

TextInput::make('title')
->label(__('filament-fabricator::page-resource.labels.title'))
Expand All @@ -85,6 +85,13 @@ public static function form(Form $form): Form
->afterStateUpdated(function (Closure $set) {
$set('is_slug_changed_manually', true);
})
->rule(function ($state) {
return function (string $attribute, $value, Closure $fail) use ($state) {
if ($state !== '/' && (Str::startsWith($value, '/') || Str::endsWith($value, '/'))) {
$fail(__('filament-fabricator::page-resource.errors.slug_starts_or_ends_with_slash'));
}
};
})
->required(),

Select::make('layout')
Expand Down Expand Up @@ -134,8 +141,8 @@ public static function table(Table $table): Table
TextColumn::make('url')
->label(__('filament-fabricator::page-resource.labels.url'))
->toggleable()
->getStateUsing(fn (?PageContract $record) => config('filament-fabricator.routing.prefix') . FilamentFabricator::getPageUrlFromId($record->id, true) ?: null)
->url(fn (?PageContract $record) => config('filament-fabricator.routing.prefix') . FilamentFabricator::getPageUrlFromId($record->id, true) ?: null, true)
->getStateUsing(fn (?PageContract $record) => FilamentFabricator::getPageUrlFromId($record->id) ?: null)
->url(fn (?PageContract $record) => FilamentFabricator::getPageUrlFromId($record->id) ?: null, true)
->visible(config('filament-fabricator.routing.enabled')),

BadgeColumn::make('layout')
Expand All @@ -160,7 +167,7 @@ public static function table(Table $table): Table
EditAction::make(),
Action::make('visit')
->label(__('filament-fabricator::page-resource.actions.visit'))
->url(fn (?PageContract $record) => config('filament-fabricator.routing.prefix') . FilamentFabricator::getPageUrlFromId($record->id, true) ?: null)
->url(fn (?PageContract $record) => FilamentFabricator::getPageUrlFromId($record->id, true) ?: null)
->icon('heroicon-o-external-link')
->openUrlInNewTab()
->color('success')
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/PageResource/Pages/EditPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function getActions(): array
Actions\DeleteAction::make(),
Action::make('visit')
->label(__('filament-fabricator::page-resource.actions.visit'))
->url(fn () => config('filament-fabricator.routing.prefix') . FilamentFabricator::getPageUrlFromId($this->record->id, true))
->url(fn () => FilamentFabricator::getPageUrlFromId($this->record->id))
->icon('heroicon-o-external-link')
->openUrlInNewTab()
->color('success')
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/PageResource/Pages/ViewPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function getActions(): array
Actions\EditAction::make(),
Action::make('visit')
->label(__('filament-fabricator::page-resource.actions.visit'))
->url(fn () => config('filament-fabricator.routing.prefix') . FilamentFabricator::getPageUrlFromId($this->record->id, true))
->url(fn () => FilamentFabricator::getPageUrlFromId($this->record->id))
->icon('heroicon-o-external-link')
->openUrlInNewTab()
->color('success')
Expand Down
9 changes: 9 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Z3d0X\FilamentFabricator\Tests;

use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
use BladeUI\Icons\BladeIconsServiceProvider;
use Filament\FilamentServiceProvider;
use Filament\Forms\FormsServiceProvider;
use Filament\Support\SupportServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
Expand All @@ -24,6 +28,11 @@ protected function getPackageProviders($app)
return [
LivewireServiceProvider::class,
FilamentServiceProvider::class,
FormsServiceProvider::class,
SupportServiceProvider::class,
BladeIconsServiceProvider::class,
BladeHeroiconsServiceProvider::class,

FilamentFabricatorServiceProvider::class,
];
}
Expand Down