Skip to content

Commit 0d2b94b

Browse files
committed
Add page routes
Add seeders
1 parent 2592729 commit 0d2b94b

11 files changed

+200
-73
lines changed

README.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/statikbe/laravel-filament-flexible-content-block-pages/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/statikbe/laravel-filament-flexible-content-block-pages/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/statikbe/laravel-filament-flexible-content-block-pages.svg?style=flat-square)](https://packagist.org/packages/statikbe/laravel-filament-flexible-content-block-pages)
77

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
9-
10-
## Support us
11-
12-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-filament-flexible-content-block-pages.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-filament-flexible-content-block-pages)
13-
14-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
15-
16-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
8+
TODO This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
179

1810
## Installation
1911

@@ -29,7 +21,7 @@ Publish the config file with:
2921
php artisan vendor:publish --tag="filament-flexible-content-block-pages-config"
3022
```
3123

32-
If you want to alter the names of the database tables, do so in the config file, before running the migrations.
24+
If you want to alter the names of the database tables, do so in the config file, **before running the migrations**.
3325

3426
You can publish and run the migrations with:
3527

@@ -38,32 +30,42 @@ php artisan vendor:publish --tag="filament-flexible-content-block-pages-migratio
3830
php artisan migrate
3931
```
4032

41-
This is the contents of the published config file:
42-
43-
```php
44-
return [
45-
];
46-
```
33+
Check [the configuration documentation}(#configuration) for more explanations on how to tweak the package.
4734

4835
Optionally, you can publish the views using
4936

5037
```bash
5138
php artisan vendor:publish --tag="filament-flexible-content-block-pages-views"
5239
```
5340

54-
## Usage
41+
## Setup in your project
42+
43+
### Routes
44+
45+
Register the routes in your route file, probably `web.php`:
5546

5647
```php
57-
$filamentFlexibleContentBlockPages = new Statikbe\FilamentFlexibleContentBlockPages();
58-
echo $filamentFlexibleContentBlockPages->echoPhrase('Hello, Statikbe!');
48+
\Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages::routes();
5949
```
6050

61-
## Testing
51+
### Filament panel
6252

63-
```bash
64-
composer test
53+
The package contains a pre-configured panel. You can register the panel in the `app.php` configuration file.
54+
55+
```php
56+
'providers' => [
57+
// ...
58+
\Statikbe\FilamentFlexibleContentBlockPages\FlexibleContentBlockPagesPanel::class,
59+
// ...
60+
],
6561
```
6662

63+
If you want you can build your own panel from the provided resources.
64+
65+
## Configuration
66+
67+
TODO
68+
6769
## Changelog
6870

6971
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

database/migrations/create_filament_flexible_content_block_pages_settings_table.php.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ return new class extends Migration {
1212
Schema::create(FilamentFlexibleContentBlockPages::config()->getSettingsTable(), function (Blueprint $table) {
1313
$table->id();
1414
$table->string(Settings::SETTING_SITE_TITLE);
15-
$table->text(Settings::SETTING_CONTACT_INFO)->nullable();
15+
$table->json(Settings::SETTING_CONTACT_INFO)->nullable();
1616
$table->json(Settings::SETTING_FOOTER_COPYRIGHT)->nullable();
1717
$table->timestamps();
1818
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
7+
class DatabaseSeeder extends Seeder
8+
{
9+
public function run(): void
10+
{
11+
$this->call([
12+
HomePageSeeder::class,
13+
SettingsSeeder::class,
14+
]);
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
7+
use Statikbe\FilamentFlexibleContentBlockPages\Models\Page;
8+
9+
class HomePageSeeder extends Seeder
10+
{
11+
public function run(): void
12+
{
13+
$locales = FilamentFlexibleContentBlockPages::config()->getSupportedLocales();
14+
$pageModel = FilamentFlexibleContentBlockPages::config()->getPageModel();
15+
16+
$homePage = new $pageModel;
17+
$homePage->code = Page::HOME_PAGE;
18+
$this->setTranslatedField($homePage, 'title', 'Home', $locales);
19+
$homePage->save();
20+
}
21+
22+
private function setTranslatedField(Page $homePage, string $field, string $value, array $locales)
23+
{
24+
foreach ($locales as $locale) {
25+
$homePage->setTranslation($field, $locale, $value);
26+
}
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
7+
use Statikbe\FilamentFlexibleContentBlockPages\Models\Settings;
8+
9+
class SettingsSeeder extends Seeder
10+
{
11+
public function run(): void
12+
{
13+
$locales = FilamentFlexibleContentBlockPages::config()->getSupportedLocales();
14+
$settingsModel = FilamentFlexibleContentBlockPages::config()->getSettingsModel();
15+
$settings = new $settingsModel;
16+
17+
$settings->site_title = config('app.name');
18+
$this->setTranslatedField($settings, 'footer_copyright', 'Made with love by Statik', $locales);
19+
20+
$settings->save();
21+
}
22+
23+
private function setTranslatedField(Settings $settings, string $field, string $value, array $locales)
24+
{
25+
foreach ($locales as $locale) {
26+
$settings->setTranslation($field, $locale, $value);
27+
}
28+
}
29+
}

src/FilamentFlexibleContentBlockPages.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22

33
namespace Statikbe\FilamentFlexibleContentBlockPages;
44

5+
use Illuminate\Support\Facades\Route;
6+
use Statikbe\FilamentFlexibleContentBlockPages\Http\Controllers\PageController;
7+
58
class FilamentFlexibleContentBlockPages
69
{
7-
public static function config(): FilamentFlexibleContentBlockPagesConfig
10+
public function config(): FilamentFlexibleContentBlockPagesConfig
811
{
912
return app(FilamentFlexibleContentBlockPagesConfig::class);
1013
}
14+
15+
public function routes(): void
16+
{
17+
Route::get('{grandparent}/{parent}/{page}', [PageController::class, 'grandchildIndex'])
18+
->name('filament-flexible-content-block-pages::child_page_index');
19+
Route::get('{parent}/{page}', [PageController::class, 'childIndex'])
20+
->name('filament-flexible-content-block-pages::child_page_index');
21+
Route::get('{page}', [PageController::class, 'index'])
22+
->name('filament-flexible-content-block-pages::page_index');
23+
Route::get('/', [PageController::class, 'homeIndex'])
24+
->name('home');
25+
}
1126
}

src/Http/Controllers/PageController.php

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
namespace Statikbe\FilamentFlexibleContentBlockPages\Http\Controllers;
44

5-
use Artesaos\SEOTools\Facades\SEOTools;
65
use Artesaos\SEOTools\Facades\SEOMeta;
7-
use Carbon\Carbon;
6+
use Artesaos\SEOTools\Facades\SEOTools;
87
use Illuminate\Foundation\Validation\ValidatesRequests;
98
use Illuminate\Http\Response;
109
use Illuminate\Routing\Controller;
1110
use Illuminate\Support\Facades\Auth;
1211
use Illuminate\Support\Facades\Cache;
13-
use Illuminate\Support\Facades\Storage;
1412
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
1513
use Spatie\MediaLibrary\Conversions\ConversionCollection;
14+
use Spatie\MediaLibrary\MediaCollections\Models\Media;
1615
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
1716
use Statikbe\FilamentFlexibleContentBlockPages\Models\Page;
1817
use Statikbe\FilamentFlexibleContentBlockPages\Models\Settings;
@@ -22,10 +21,11 @@ class PageController extends Controller
2221
use ValidatesRequests;
2322

2423
const CACHE_SEO_IMAGE_DIMENSIONS = 'seo_image_dimensions:%s';
24+
2525
const CACHE_SEO_IMAGE_TTL = 60 * 60 * 8; // in seconds
26-
const TEMPLATE_PATH = 'filament-flexible-content-block-pages::pages.';
2726

28-
// TODO make an abstract model with the table name to use as class to resolve in the route param
27+
const TEMPLATE_PATH = 'filament-flexible-content-block-pages::pages.index';
28+
2929
public function index(Page $page)
3030
{
3131
// check if page is published:
@@ -41,11 +41,41 @@ public function index(Page $page)
4141
$this->setSEOLocalisationAndCanonicalUrl();
4242
$this->setSEOImage($page);
4343

44-
return view(self::TEMPLATE_PATH.'index', [
44+
return view(self::TEMPLATE_PATH, [
4545
'page' => $page,
4646
]);
4747
}
4848

49+
public function homeIndex()
50+
{
51+
$page = Page::code(Page::HOME_PAGE)
52+
->firstOrFail();
53+
54+
return $this->index($page);
55+
}
56+
57+
public function childIndex(Page $parent, Page $page)
58+
{
59+
// check if the page is a child of the parent
60+
if (! $parent->isParentOf($page)) {
61+
abort(Response::HTTP_NOT_FOUND);
62+
}
63+
64+
// render the page with the regular page index function of the controller, or invoke the correct controller here:
65+
return $this->index($page);
66+
}
67+
68+
public function grandchildIndex(Page $grandparent, Page $parent, Page $page)
69+
{
70+
// check if the page is a child of the parent
71+
if (! $parent->isParentOf($page) || ! $grandparent->isParentOf($parent)) {
72+
abort(Response::HTTP_NOT_FOUND);
73+
}
74+
75+
// render the page with the regular page index function of the controller, or invoke the correct controller here:
76+
return $this->index($page);
77+
}
78+
4979
protected function getSEOTitlePostfix()
5080
{
5181
return sprintf(' | %s', flexiblePagesSetting(Settings::SETTING_SITE_TITLE));
@@ -98,37 +128,35 @@ protected function setBasicSEO(Page $page)
98128

99129
protected function setSEOImage(Page $page)
100130
{
101-
//TODO copy getSEOMedia in HasSEOAttributesTrait
102131
$seoMedia = $page->getFallbackImageMedia($page->SEOImage()->first(), $page->getSEOImageCollection());
103132
$seoUrl = null;
104133
$imageDimensions = null;
105134

106-
/*if ($seoMedia) {
135+
// 1. try SEO image of the page
136+
if ($seoMedia) {
107137
$seoUrl = $seoMedia->getUrl($page->getSEOImageConversionName());
108138
$imageDimensions = $this->getSEOImageDimensions($seoMedia, $page->getSEOImageConversionName());
109-
} else if()
110-
$seoUrl = $seoMedia->getUrl($page->getSEOImageConversionName());
111-
$imageDimensions = $this->getSEOImageDimensions($seoMedia, $page->getSEOImageConversionName());
112-
$this->setSEODefaultImage($page);
139+
} else {
140+
// 2. try the hero image of the page
141+
$seoMedia = $page->getFallbackImageMedia($page->heroImage()->first(), $page->getHeroImageCollection());
142+
if ($seoMedia) {
143+
$seoUrl = $seoMedia->getUrl($page->getSEOImageConversionName());
144+
$imageDimensions = $this->getSEOImageDimensions($seoMedia, $page->getSEOImageConversionName());
145+
}
146+
147+
// 3. try the default SEO image in the settings
148+
if (! $seoMedia || ! $seoUrl) {
149+
/** @var Settings $settings */
150+
$settings = FilamentFlexibleContentBlockPages::config()->getSettingsModel()::getSettings();
151+
$seoMedia = $settings->getFallbackImageMedia($settings->defaultSeoImage()->first(), $settings::COLLECTION_DEFAULT_SEO);
152+
$seoUrl = $seoMedia->getUrl($settings::CONVERSION_DEFAULT_SEO);
153+
$imageDimensions = $this->getSEOImageDimensions($seoMedia, $settings::CONVERSION_DEFAULT_SEO);
154+
}
113155
}
114156

115-
if($seoUrl && $imageDimensions) {
157+
if ($seoUrl && $imageDimensions) {
116158
SEOTools::opengraph()->addImage($seoUrl, $imageDimensions);
117159
SEOTools::twitter()->addValue('image', $seoUrl);
118-
}*/
119-
}
120-
121-
protected function setSEODefaultImage(Page $page)
122-
{
123-
$defaultSeoImage = flexiblePagesSettingImageUrl(Settings::COLLECTION_DEFAULT_SEO, Settings::CONVERSION_DEFAULT_SEO);
124-
// $test = Storage::get($defaultSeoImage);
125-
if ($defaultSeoImage && trim($defaultSeoImage) != '') {
126-
$seoParams = $this->getSEOImageDimensions($defaultSeoImage, true);
127-
$this->setSEOImage($defaultSeoImage, $seoParams);
128-
} else {
129-
$imageUrl = $page->getHeroImageUrl($page->getSEOImageConversionName());
130-
$seoParams = $this->getSEOImageDimensions($imageUrl, true);
131-
$this->setSEOImage($imageUrl, $seoParams);
132160
}
133161
}
134162

@@ -143,7 +171,7 @@ protected function getSEOImageDimensions(Media $seoMedia, string $conversion)
143171

144172
return Cache::remember($cacheKey,
145173
self::CACHE_SEO_IMAGE_TTL,
146-
function() use ($seoMedia, $conversion) {
174+
function () use ($seoMedia, $conversion) {
147175
$conversionCollection = ConversionCollection::createForMedia($seoMedia);
148176
$conversion = $conversionCollection->getByName($conversion);
149177

src/Listeners/SlugChangedListener.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Statikbe\FilamentFlexibleContentBlockPages\Listeners;
44

5+
use Illuminate\Http\Response;
56
use Statikbe\FilamentFlexibleContentBlockPages\Models\Page;
67
use Statikbe\FilamentFlexibleContentBlockPages\Models\Redirect;
7-
use Illuminate\Http\Response;
88
use Statikbe\FilamentFlexibleContentBlocks\Events\SlugChanged;
99

1010
/**
@@ -14,31 +14,30 @@ class SlugChangedListener
1414
{
1515
public function handle(SlugChanged $event): void
1616
{
17-
//add redirect:
18-
if($event->recordWasPublished) {
19-
foreach ($event->changedSlugs as $changedSlug){
17+
// add redirect:
18+
if ($event->recordWasPublished) {
19+
foreach ($event->changedSlugs as $changedSlug) {
2020
$oldUrl = null;
2121
$newUrl = null;
2222

23-
if($changedSlug['newSlug'] && !empty(trim($changedSlug['newSlug']))) {
23+
if ($changedSlug['newSlug'] && ! empty(trim($changedSlug['newSlug']))) {
2424
if ($event->record instanceof Page) {
2525

2626
$oldUrl = $this->getUrl($event->record, $changedSlug['locale'], $changedSlug['oldSlug']);
2727
$newUrl = $this->getUrl($event->record, $changedSlug['locale'], $changedSlug['newSlug']);
2828
}
2929
}
3030

31-
32-
if($newUrl && $oldUrl){
31+
if ($newUrl && $oldUrl) {
3332
$oldUrlPath = parse_url($oldUrl, PHP_URL_PATH);
3433
$newUrlPath = parse_url($newUrl, PHP_URL_PATH);
3534

3635
$redirectDoesNotExist = Redirect::where('old_url', $oldUrlPath)
3736
->where('new_url', $newUrlPath)
3837
->notExists();
3938

40-
if($redirectDoesNotExist) {
41-
$redirect = new Redirect();
39+
if ($redirectDoesNotExist) {
40+
$redirect = new Redirect;
4241
$redirect->old_url = $oldUrlPath;
4342
$redirect->new_url = $newUrlPath;
4443
$redirect->status_code = Response::HTTP_MOVED_PERMANENTLY;

src/Models/Page.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Page extends Model implements HasCode, HasContentBlocks, HasHeroImageAttri
4545
use HasTranslatedSEOAttributesTrait;
4646
use HasTranslatedSlugAttributeTrait;
4747

48+
const HOME_PAGE = 'HOME';
49+
4850
public function getTable()
4951
{
5052
return FilamentFlexibleContentBlockPages::config()->getPagesTable();

0 commit comments

Comments
 (0)