Api Reference - Streats22/LaraGrapePackage GitHub Wiki

🔧 API Reference

Comprehensive documentation for LaraGrape service classes, methods, and integration points.

📦 Service Classes

BlockService

The BlockService handles dynamic block loading, management, and GrapesJS integration.

Location

App\Services\BlockService

Key Methods

getBlocks()

Returns all available blocks organized by category.

use App\Services\BlockService;

$blockService = new BlockService();
$blocks = $blockService->getBlocks();

// Returns:
[
    'components' => [
        [
            'id' => 'button',
            'label' => 'Button',
            'content' => '<button class="btn">Click me</button>',
            'category' => 'components'
        ],
        // ... more blocks
    ],
    'layouts' => [
        // ... layout blocks
    ],
    // ... more categories
]
getGrapesJsBlocks()

Returns blocks formatted for GrapesJS block manager.

$grapesJsBlocks = $blockService->getGrapesJsBlocks();

// Returns array of blocks with GrapesJS format:
[
    [
        'id' => 'button',
        'label' => 'Button',
        'content' => '<button class="btn">Click me</button>',
        'category' => 'components',
        'attributes' => [
            'draggable' => true,
            'droppable' => true,
            'removable' => true,
            'copyable' => true
        ]
    ]
]
renderBlockPreview($blockId)

Renders a block preview for the GrapesJS editor.

$preview = $blockService->renderBlockPreview('button');
// Returns HTML string of the block
findBlockFileById($blockId)

Finds the Blade file path for a block by ID.

$filePath = $blockService->findBlockFileById('button');
// Returns: /path/to/resources/views/filament/blocks/components/button.blade.php

Block Discovery

The service automatically scans for blocks in:

resources/views/filament/blocks/
├── components/
├── content/
├── forms/
├── layouts/
└── media/

Block Metadata

Blocks are defined with metadata comments:

{{-- @block id="my-block" label="My Block" description="Description" --}}
<div class="my-block">
    <h3 data-gjs-type="text" data-gjs-name="title">Title</h3>
</div>

SiteSettingsService

The SiteSettingsService manages site-wide configuration and settings.

Location

App\Services\SiteSettingsService

Key Methods

getSettings()

Returns all site settings as an array.

use App\Services\SiteSettingsService;

$siteSettings = new SiteSettingsService();
$settings = $siteSettings->getSettings();

// Returns all settings from database
getSetting($key, $default = null)

Gets a specific setting by key.

$siteName = $siteSettings->getSetting('site_name', 'Default Site Name');
$logoUrl = $siteSettings->getSetting('logo_url');
getHeaderSettings()

Returns header-specific settings.

$headerSettings = $siteSettings->getHeaderSettings();

// Returns:
[
    'logo_text' => 'Site Name',
    'logo_image' => '/path/to/logo.png',
    'background_color' => '#ffffff',
    'text_color' => '#000000',
    'sticky_header' => true,
    'show_search' => false
]
getFooterSettings()

Returns footer-specific settings.

$footerSettings = $siteSettings->getFooterSettings();

// Returns:
[
    'footer_text' => '© 2024 Site Name',
    'background_color' => '#f8f9fa',
    'text_color' => '#6c757d',
    'show_social' => true,
    'social_links' => [
        'facebook' => 'https://facebook.com/site',
        'twitter' => 'https://twitter.com/site'
    ]
]
getSeoSettings()

Returns SEO-related settings.

$seoSettings = $siteSettings->getSeoSettings();

// Returns:
[
    'default_title' => 'Site Name',
    'default_description' => 'Site description',
    'default_keywords' => 'keyword1, keyword2',
    'google_analytics' => 'GA_TRACKING_ID'
]
getAllCss()

Generates CSS from settings for dynamic styling.

$css = $siteSettings->getAllCss();

// Returns CSS string with dynamic values from settings

Setting Categories

  • General: Site name, contact info, address
  • Header: Logo, colors, navigation settings
  • Footer: Footer content, social links
  • SEO: Meta tags, analytics
  • Social: Social media URLs
  • Advanced: Custom CSS, JavaScript

GrapesJsConverterService

The GrapesJsConverterService handles conversion between GrapesJS data and HTML.

Location

App\Services\GrapesJsConverterService

Key Methods

convertToHtml($grapesJsData)

Converts GrapesJS JSON data to HTML.

use App\Services\GrapesJsConverterService;

$converter = new GrapesJsConverterService();
$html = $converter->convertToHtml($grapesJsJsonData);

// Returns rendered HTML string
convertToGrapesJs($html)

Converts HTML to GrapesJS format (if needed).

$grapesJsData = $converter->convertToGrapesJs($html);
extractStyles($grapesJsData)

Extracts CSS styles from GrapesJS data.

$styles = $converter->extractStyles($grapesJsData);

// Returns CSS string

🎯 Model Classes

Page Model

Location

App\Models\Page

Key Properties

protected $fillable = [
    'title',
    'slug',
    'content',
    'grapesjs_data',
    'blade_content',
    'meta_title',
    'meta_description',
    'meta_keywords',
    'is_published',
    'published_at',
    'template'
];

Key Methods

getRouteKeyName()

Returns 'slug' for route model binding.

scopePublished($query)

Scope for published pages only.

$publishedPages = Page::published()->get();

CustomBlock Model

Location

App\Models\CustomBlock

Key Properties

protected $fillable = [
    'name',
    'description',
    'category',
    'html_content',
    'css_content',
    'js_content',
    'grapesjs_attributes',
    'is_active',
    'sort_order'
];

SiteSettings Model

Location

App\Models\SiteSettings

Key Properties

protected $fillable = [
    'key',
    'value',
    'type',
    'group'
];

TailwindConfig Model

Location

App\Models\TailwindConfig

Key Methods

generateUtilityClassesCss()

Generates CSS for utility classes.

generateSiteThemeCss()

Generates CSS for site theme.

generateAdminThemeCss()

Generates CSS for admin theme.


🛠️ Controller Classes

PageController

Location

App\Http\Controllers\PageController

Key Methods

show($slug)

Displays a page by slug.

public function show($slug)
{
    $page = Page::where('slug', $slug)->published()->firstOrFail();
    return view('pages.show', compact('page'));
}

AdminPageController

Location

App\Http\Controllers\AdminPageController

Key Methods

blockPreview($blockId)

Serves block previews for GrapesJS.

public function blockPreview($blockId)
{
    $blockService = new BlockService();
    $preview = $blockService->renderBlockPreview($blockId);
    
    return response($preview)->header('Content-Type', 'text/html');
}

🎨 Filament Resources

PageResource

Location

App\Filament\Resources\PageResource

Key Features

  • Tabbed interface (Basic Info, Visual Editor, Content, SEO)
  • GrapesJS editor integration
  • SEO management
  • Publishing controls

CustomBlockResource

Location

App\Filament\Resources\CustomBlockResource

Key Features

  • Visual block builder
  • Live preview
  • HTML/CSS/JS editors
  • GrapesJS integration

SiteSettingsResource

Location

App\Filament\Resources\SiteSettingsResource

Key Features

  • Grouped settings interface
  • Color pickers
  • File uploads
  • Dynamic form fields

TailwindConfigResource

Location

App\Filament\Resources\TailwindConfigResource

Key Features

  • Color palette management
  • Typography settings
  • Spacing configuration
  • Theme generation

🔧 Artisan Commands

LaraGrapeSetupCommand

Usage

php artisan laragrape:setup [options]

Options

  • --all: Complete setup
  • --migrate: Run migrations
  • --seed: Run seeders
  • --force: Overwrite files

RebuildTailwindCommand

Usage

php artisan tailwind:rebuild

Features

  • Generates dynamic CSS from TailwindConfig
  • Creates utility classes
  • Updates theme files
  • Copies to public directory

🎯 Integration Examples

Using Services in Controllers

use App\Services\BlockService;
use App\Services\SiteSettingsService;

class HomeController extends Controller
{
    public function index(BlockService $blockService, SiteSettingsService $siteSettings)
    {
        $blocks = $blockService->getGrapesJsBlocks();
        $headerSettings = $siteSettings->getHeaderSettings();
        
        return view('home', compact('blocks', 'headerSettings'));
    }
}

Using Services in Blade Views

@php
    $siteSettings = app(\App\Services\SiteSettingsService::class);
    $headerSettings = $siteSettings->getHeaderSettings();
@endphp

<header style="background-color: {{ $headerSettings['background_color'] }}">
    {{ $headerSettings['logo_text'] }}
</header>

Custom Block Integration

use App\Services\BlockService;

$blockService = new BlockService();
$customBlocks = $blockService->getBlocks();

// Add to GrapesJS
foreach ($customBlocks as $category => $blocks) {
    foreach ($blocks as $block) {
        // Add to GrapesJS block manager
    }
}

🔍 Error Handling

All services include comprehensive error handling:

try {
    $blocks = $blockService->getBlocks();
} catch (\Exception $e) {
    Log::error('Failed to load blocks: ' . $e->getMessage());
    $blocks = []; // Fallback to empty array
}

📚 Related Documentation

  • Installation Guide
  • [Block System](Block System.md)
  • [Component System](Block System.md)
  • [Custom Blocks](Custom Block System.md)
  • [Troubleshooting](Common Issues.md)

The API is designed to be intuitive, extensible, and well-documented for easy integration into your Laravel applications! 🔧

⚠️ **GitHub.com Fallback** ⚠️