optimistdigital / nova-page-manager
Page(s) and region(s) manager for Laravel Nova.
Fund package maintenance!
outl1ne
Installs: 82 808
Dependents: 0
Suggesters: 0
Security: 0
Stars: 180
Watchers: 7
Forks: 39
Open Issues: 8
Requires
- php: >=8.0
- laravel/nova: ^4.13
- outl1ne/nova-multiselect-filter: ^4.0.2
- outl1ne/nova-translatable: ^2.0.4
- outl1ne/nova-translations-loader: ^5.0
- dev-main
- 5.9.3
- 5.9.2
- 5.9.1
- 5.9.0
- 5.8.6
- 5.8.5
- 5.8.4
- 5.8.3
- 5.8.2
- 5.8.1
- 5.8.0
- 5.8.0-RC3
- 5.8.0-RC2
- 5.8.0-RC1
- 5.7.x-dev
- 5.7.3
- 5.7.2
- 5.7.1
- 5.7.0
- 5.6.2
- 5.6.1
- 5.6.0
- 5.5.4
- 5.5.3
- 5.5.2
- 5.5.1
- 5.5.0
- 5.4.3
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.4
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- v4.x-dev
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.9.9
- 1.9.8
- 1.9.7
- 1.9.6
- 1.9.5
- 1.9.4
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/npm_and_yarn/npm_and_yarn-af93afb32e
- dev-bugfix/handle-nested-fields
- dev-feature/validation-support
This package is auto-updated.
Last update: 2024-12-14 11:43:31 UTC
README
This Laravel Nova package allows you to create and manage pages and regions for your frontend application.
Requirements
- PHP >=8.0
- laravel/nova ^4.13
Features
- Page and region management w/ custom fields
- Multiple locale support
Screenshots
Installation
Install the package in a Laravel Nova project via Composer and run migrations:
# Install package composer require outl1ne/nova-page-manager # Run automatically loaded migrations php artisan migrate
Publish the nova-page-manager
configuration file and edit it to your preference:
php artisan vendor:publish --provider="Outl1ne\PageManager\NPMServiceProvider" --tag="config"
Register the tool with Nova in the tools()
method of the NovaServiceProvider
:
// in app/Providers/NovaServiceProvider.php public function tools() { return [ // ... new \Outl1ne\PageManager\PageManager() ->withSeoFields(fn () => []), // Optional ]; }
Usage
Creating templates
Templates can be created using the following Artisan command:
php artisan npm:template {className}
This will ask you a few additional details and will create a base template in App\Nova\Templates
.
The base template exposes a few overrideable functions:
// Name displayed in CMS public function name(Request $request) { return parent::name($request); } // Fields displayed in CMS public function fields(Request $request): array { return []; } // Resolve data for serialization public function resolve($page): array { // Modify data as you please (ie turn ID-s into models) return $page->data; } // Page only // Optional suffix to the route (ie {blogPostName}) public function pathSuffix() { return null; }
Registering templates
All your templates have to be registered in the config/nova-page-manager.php
file.
// in /config/nova-page-manager.php // ... 'templates' => [ 'pages' => [ 'home-page' => [ 'class' => '\App\Nova\Templates\HomePageTemplate', 'unique' => true, // Whether more than one page can be created with this template ], ], 'regions' => [ 'header' => [ 'class' => '\App\Nova\Templates\HeaderRegionTemplate', 'unique' => true, ], ], ], // ...
Defining locales
The locales are defined in the config file.
// in /config/nova-page-manager.php // ... 'locales' => [ 'en' => 'English', 'et' => 'Estonian', ], // OR 'locales' => function () { return Locale::all()->pluck('name', 'key'); }, // or if you wish to cache the configuration, pass a function name instead: 'locales' => NPMConfiguration::class . '::locales', // ...
Add links to front-end pages
To display a link to the actual page next to the slug, add or overwrite the closure in config/nova-page-manager.php
for the key base_url
.
// in /config/nova-page-manager.php 'base_url' => 'https://webshop.com', // Will add slugs to the end to make the URLs // OR 'base_url' => function ($page) { return env('FRONTEND_URL') . '/' . $page->path; },
Overwriting models and resources
You can overwrite the page/region models or resources, just set the new classes in the config file.
Custom locale display
To customize the locale display you can use Nova::provideToScript
to pass customLocaleDisplay
as in the example below.
// in app/Providers/NovaServiceProvider.php public function boot() { Nova::serving(function () { Nova::provideToScript([ // ... 'customLocaleDisplay' => [ 'en' => <img src="/flag-en.png"/>, 'et' => <img src="/flag-et.png"/>, ] ]); }); }
Advanced usage
Non-translatable panels
There's some cases where it's more sensible to translate sub-fields of a panel instead of the whole panel. This is possible, but is considered an "advanced usecase" as the feature is really new and experimental, also the developer experience of it is questionable.
You can create a non-translatable panel like so:
// In your PageTemplate class public function fields() { return [ Panel::make('Some panel', [ Text::make('Somethingsomething'), Text::make('Sub-translatable', 'subtranslatable') ->translatable(), ]) ->translatable(false), ]; }
This will create a key with __
in the page data object. This means that the page data will end up looking something like this:
[ '__' => [ 'somethingsomething' => 'your value', 'subtranslatable' => [ 'en' => 'eng value', 'et' => 'et value' ] ], 'en' => [], 'et' => [], ]
Helper functions
Helper functions can be found in the Outl1ne\PageManager\Helpers\NPMHelpers
class.
NPMHelpers::getPagesStructure()
Calls resolve()
on their template class and returns all pages as a tree where child pages are nested inside the children
array key recursively.
NPMHelpers::getPages()
Calls resolve()
on their template class and returns all pages. Returns an array of arrays.
NPMHelpers::getRegions()
Calls resolve()
on their template class and returns all regions. Returns an array of arrays.
NPMHelpers::getPageByTemplate($templateSlug)
Finds a single page by its template slug (from the config file), calls resolve()
on its template class and returns it.
NPMHelpers::getPagesByTemplate($templateSlug)
Same as getPageByTemplate
, but returns an array of pages.
NPMHelpers::formatPage($page)
Calls resolve()
on the page's template class and returns the page as an array.
NPMHelpers::formatRegion($region)
Calls resolve()
on the region's template class and returns the region as an array.
Localization
The translation file(s) can be published by using the following command:
php artisan vendor:publish --provider="Outl1ne\PageManager\ToolServiceProvider" --tag="translations"
You can add your translations to resources/lang/vendor/nova-page-manager/
by creating a new translations file with the locale name (ie et.json
) and copying the JSON from the existing en.json
.
Credits
License
Nova page manager is open-sourced software licensed under the MIT license.