Universal blocks system for Laravel (static & dynamic content).
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',
],
],
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;
}
}
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.