PHP code example of outl1ne / nova-page-manager

1. Go to this page and download the library: Download outl1ne/nova-page-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

outl1ne / nova-page-manager example snippets


// in app/Providers/NovaServiceProvider.php

public function tools()
{
    return [
        // ...
        new \Outl1ne\PageManager\PageManager()
          ->withSeoFields(fn () => []), // Optional
    ];
}

// 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;
}

// 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,
        ],
    ],
],
// ...

// 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',
// ...

// in /config/nova-page-manager.php

'base_url' => 'https://webshop.com', // Will add slugs to the end to make the URLs

// 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"/>,
            ]
        ]);
    });
}

// In your PageTemplate class

public function fields() {
  return [
    Panel::make('Some panel', [
      Text::make('Somethingsomething'),
      Text::make('Sub-translatable', 'subtranslatable')
        ->translatable(),
    ])
    ->translatable(false),
  ];
}

[
  '__' => [
    'somethingsomething' => 'your value',
    'subtranslatable' => [
      'en' => 'eng value',
      'et' => 'et value'
    ]
  ],
  'en' => [],
  'et' => [],
]

Route::get('/page/{path?}', [PageController::class, 'show'])
    ->where('path', '[\w-]+');

public function show(Request $request, $path = '/')
    {
        
        // Get the current locale
        $locale = App::getLocale();
        $locales = NPM::getLocales();
        
        // Force a valid locale
        if (!isset($locales[$locale])) {
            $locale = array_key_first($locales);
        }
        
        // get page model class
        $pageModel = NPM::getPageModel();
        
        // get the model istance
        $page = $pageModel::where('active', true)
        ->where(function($query) use ($locales, $path) {
            foreach($locales as $locale => $name){
                $query->orWhere('slug->' . $locale, $path);
            }
        })
        ->first();
        
        // return 404 if page not found
        if (!$page) {
            abort(404);
        }
        
        // Get the page template
        $templateSlug = $page->template;
        
        
        // Prepare page data
        // maybe here you should filter the data by the locale
        $pageData = $page->toArray();
        
        // Render the dynamic page component
        return Inertia::render('DynamicPage', [
            'page' => $pageData,
            'template' => $templateSlug,
            //maybe here you're interested also in regions
            //'regions' => NPM::getRegions(),
        ]);

        //OR

        //  return view('page', [
        //     'page' => $pageData,
        //     'template' => $templateSlug,
        // ]);

        //OR

        //  return view('page.' . $templateSlug, [
        //     'page' => $pageData,
        // ]);

        // ...
    }

- PHP >=8.0
- laravel/nova ^4.13
bash
php artisan vendor:publish --provider="Outl1ne\PageManager\NPMServiceProvider" --tag="config"
bash
php artisan npm:template {className}
bash
php artisan vendor:publish --provider="Outl1ne\PageManager\ToolServiceProvider" --tag="translations"