fomvasss / laravel-blocks
Universal blocks system for Laravel (static & dynamic content)
Installs: 7
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/fomvasss/laravel-blocks
Requires
- php: ^8.0
- illuminate/contracts: ^9|^10|^11|^12|^13
- intervention/image: ^3
README
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.