GithubHelp home page GithubHelp logo

marttinnotta / nova-page-manager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from outl1ne/nova-page-manager

0.0 0.0 0.0 5.11 MB

Static page and region manager for Laravel Nova - designed for headless CMS's.

License: MIT License

JavaScript 2.50% PHP 79.74% CSS 0.03% Vue 17.73%

nova-page-manager's Introduction

Nova Page Manager

Latest Version on Packagist Total Downloads

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

Form (dark)

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.

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.

nova-page-manager's People

Contributors

tarpsvo avatar kasparrosin avatar mikkoun avatar marttinnotta avatar kaareloun avatar allantatter avatar sloveniangooner avatar shahruslan avatar dependabot[bot] avatar lvdhoorn avatar trippo avatar ribesalexandre avatar ferdiunal avatar akiyamasm avatar jgile avatar kakajansh avatar kikoseijo avatar liorocks avatar marcelosantos89 avatar murad-optimist avatar stephenlake avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.