PHP code example of coderstm / laravel-page-builder

1. Go to this page and download the library: Download coderstm/laravel-page-builder 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/ */

    

coderstm / laravel-page-builder example snippets


return [
    // Base URL path for the page builder routes (e.g., 'pagebuilder' or 'foo')
    'prefix' => 'pagebuilder',

    // The prefix for the page builder public pages and editor.
    // If your pages are served at /foo/*, set this to 'foo'.
    'basePath' => '/',

    // Additional query parameters to preserve during editor navigation
    'preserved_params' => [],

    // Path to page JSON data files
    'pages' => resource_path('views/pages'),

    // Path to section Blade templates
    'sections' => resource_path('views/sections'),

    // Path to theme block Blade templates
    'blocks' => resource_path('views/blocks'),

    // Path to JSON template files (fallback layouts for pages without a page JSON)
    'templates' => resource_path('views/templates'),

    // Middleware applied to editor routes
    'middleware' => ['web'],

    // Filesystem disk for asset uploads
    'disk' => 'public',

    // Directory within the disk for uploaded assets
    'asset_directory' => 'pagebuilder',

    // Reserved slugs that cannot be used for dynamic pages
    'preserved_pages' => ['home', 'admin', 'user', 'api', 'storage', 'uploads', 'files', 'vendor'],

    // Path to the JSON file storing theme setting values
    'theme_settings_path' => resource_path('settings.json'),
];

$page = Page::find(1);
$page->template = 'page.alternate';
$page->save();

Page::create([
    'title'    => 'About Us',
    'slug'     => 'about',
    'template' => 'page.alternate',
    'content'  => '<p>About our company.</p>',
]);

use Coderstm\PageBuilder\Facades\Page;

class PageController extends Controller
{
    public function show(string $slug)
    {
        return Page::render($slug);
    }
}

use Coderstm\PageBuilder\Facades\Page;

// Render from slug (loads JSON from disk)
$html = Page::render('home');

// Render with extra meta passed to the page model/template
$html = Page::render('home', ['title' => 'My Home Page']);

use Coderstm\PageBuilder\Facades\Section;
use Coderstm\PageBuilder\Facades\Block;

// In a service provider's boot() method
Section::add(resource_path('views/custom-sections'));
Block::add(resource_path('views/custom-blocks'));

use Coderstm\PageBuilder\Facades\Section;
use Coderstm\PageBuilder\Schema\SectionSchema;

Section::register('custom-hero', new SectionSchema([
    'name' => 'Custom Hero',
    'settings' => [
        ['id' => 'title', 'type' => 'text', 'label' => 'Title', 'default' => 'Hello'],
    ],
]), 'my-views::sections.custom-hero');

// config/pagebuilder.php
'middleware' => ['web', 'auth'],

use Coderstm\PageBuilder\PageBuilder;

public function boot()
{
    PageBuilder::auth(function ($request) {
        // Return true if the user is authorized to access the editor
        return auth()->check() && auth()->user()->is_admin;
    });
}

// Check if editor mode is active
pb_editor(); // Returns bool

// In Blade templates
@if(pb_editor())
    {{-- Editor-only content --}}
@endif

use Coderstm\PageBuilder\Facades\Theme;
use Coderstm\PageBuilder\Facades\Section;
use Coderstm\PageBuilder\Facades\Block;

// Set the active theme (for example in a ThemeServiceProvider or middleware)
Theme::set('my-theme');

// The package will automatically register the following directories if they exist:
//   themes/my-theme/views/sections
//   themes/my-theme/views/blocks

// If you need to register additional paths manually you can still call:
Section::add(base_path('themes/my-theme/views/sections'));
Block::add(base_path('themes/my-theme/views/blocks'));

'theme_settings_schema' => [
    [
        'name' => 'Colors',
        'settings' => [
            [
                'key'     => 'colors.primary',
                'label'   => 'Primary',
                'type'    => 'color',
                'default' => '#10b981',
                'css_var' => '--colors-primary',
            ],
            [
                'key'     => 'colors.background_dark',
                'label'   => 'Background (dark)',
                'type'    => 'color',
                'default' => '#0f0f0f',
                'css_var' => '--colors-background-dark',
            ],
        ],
    ],
    [
        'name' => 'Typography',
        'settings' => [
            [
                'key'     => 'fonts.body',
                'label'   => 'Body font',
                'type'    => 'google_font',
                'default' => 'Inter, sans-serif',
                'css_var' => '--fonts-body',
            ],
        ],
    ],
    [
        'name' => 'Radius & Shape',
        'settings' => [
            [
                'key'     => 'radius.base',
                'label'   => 'Radius (base)',
                'type'    => 'text',
                'default' => '0.25rem',
                'css_var' => '--radius-base',
            ],
        ],
    ],
],

// routes/web.php
Route::get('/shop/{theme_slug}', function () {
    // ...
})->middleware('theme:theme_slug');
bash
php artisan pagebuilder:install
bash
php artisan migrate
bash
# Config
php artisan vendor:publish --tag=pagebuilder-config

# Database migrations
php artisan vendor:publish --tag=pagebuilder-migrations

# Editor frontend assets (React SPA)
php artisan vendor:publish --tag=pagebuilder-assets

# Built-in package views
php artisan vendor:publish --tag=pagebuilder-views