Skip to content

fomvasss/laravel-blocks

Repository files navigation

Laravel Blocks package

License Build Status Latest Stable Version Total Downloads Quality Score

Universal blocks system for Laravel (static & dynamic content).

Installation

Install the package via composer:

composer require fomvasss/laravel-blocks

Publish and run the migrations with:

php artisan vendor:publish --provider="Fomvasss\Blocks\ServiceProvider"
php artisan migrate

Add to filesystems.php disk config (for cache images):

    'disks' => [
    //...
        'blocks' => [
            'driver' => 'local',
            'root' => storage_path('app/public/blocks'),
            'url' => env('APP_URL').'/storage/blocks',
            'visibility' => 'public',
        ],
    ],

Usage

The Eloquent model for relations must has the Trait HasBlocks:

namespace App\Models;

use Fomvasss\Blocks\Models\HasBlocks;

class PageModel extends Model {

	use HasBlocks;
	//...
}

Use facede

 \Block::setAttrs(['sort' => 'desc'])->init('some-1', 'slug')->getBlock();
 
 \Block::init('contacts')->getData('phone');
 
 \Block::init('slider')->getDataSort('slides');

For prepare dinamic block content, place your hendlers in dir app/Blocks/...

Example app/Blocks/ContactsBlockHandler.php:

<?php

namespace App\Blocks;

use Fomvasss\Blocks\Contracts\BlockHandlerInterface;
use Illuminate\Database\Eloquent\Model;

class ContactsBlockHandler implements BlockHandlerInterface
{

    public static function getType(): string
    {
        return 'contacts';
    }

    public function handle(Model $block, array $attrs = []): array
    {
        return [
            'email' => config('app.email'),
            'address' => $block->getContent('address', ''),
            'phone' => preg_replace('/[^0-9]/si', '', $block->getContent('phone', '')),
        ] + $attrs;
    }
}

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages