krafthaus/bauhausblock
Laravel package for building “Bauhaus” content blocks/components for your app or CMS. Provides a structured way to define, render, and manage reusable blocks, helping you compose pages from modular content with minimal setup.
Installation:
composer require krafthaus/bauhausblock
Publish the config (if needed):
php artisan vendor:publish --provider="Krafthaus\Bauhausblock\BauhausblockServiceProvider"
Basic Usage:
use Krafthaus\Bauhausblock\Block;
Schema::create('blocks', function (Blueprint $table) {
$table->block('hero_section', Block::class);
});
php artisan make:model Block/HeroSection
@block('hero_section')
First Use Case:
@block('hero_section', ['title' => 'Welcome', 'content' => 'This is a hero section'])
$block = \Krafthaus\Bauhausblock\Facades\Bauhausblock::find(1);
Block Definition:
block() method:
$table->block('sidebar', Block::class, ['default' => ['title' => 'Default']]);
use Krafthaus\Bauhausblock\Traits\HasBlocks;
Block Management:
$block = \Krafthaus\Bauhausblock\Facades\Bauhausblock::create('hero_section', [
'title' => 'New Hero',
'content' => 'Updated content'
]);
// By name
$blocks = \Krafthaus\Bauhausblock\Facades\Bauhausblock::get('hero_section');
// By ID
$block = \Krafthaus\Bauhausblock\Facades\Bauhausblock::find(1);
View Integration:
@block('hero_section', ['title' => 'Dynamic Title'])
@if($block = \Krafthaus\Bauhausblock\Facades\Bauhausblock::find(1))
@block('hero_section', $block->data)
@endif
API Endpoints:
Route::get('/blocks/{block}', [BlockController::class, 'show']);
public function show($blockName)
{
return \Krafthaus\Bauhausblock\Facades\Bauhausblock::get($blockName);
}
$blocks = Cache::remember("blocks_{$blockName}", now()->addHours(1), function () use ($blockName) {
return \Krafthaus\Bauhausblock\Facades\Bauhausblock::get($blockName);
});
\Krafthaus\Bauhausblock\Events\BlockUpdated::class => [BlockObserver::class, 'handleUpdate']
public function rules()
{
return [
'title' => 'required|string|max:255',
'content' => 'required|string',
];
}
Migration Conflicts:
blocks table migration.Data Serialization:
// Bad: Non-serializable (e.g., DateTime objects)
$block = ['date' => new DateTime()];
// Good: Convert to string or array
$block = ['date' => (new DateTime())->format('Y-m-d')];
Facade vs. Direct Usage:
\Krafthaus\Bauhausblock\Facades\Bauhausblock) for simplicity, but inject the service container binding (\Krafthaus\Bauhausblock\Bauhausblock) for testing or complex logic.Default Values:
$block = \Krafthaus\Bauhausblock\Facades\Bauhausblock::create('hero_section', [
'title' => 'Override Default',
'content' => 'New content'
]);
Query Logs:
\DB::enableQueryLog();
$blocks = \Krafthaus\Bauhausblock\Facades\Bauhausblock::get('hero_section');
dd(\DB::getQueryLog());
Block Existence:
if (\Krafthaus\Bauhausblock\Facades\Bauhausblock::hasType('hero_section')) {
$blocks = \Krafthaus\Bauhausblock\Facades\Bauhausblock::get('hero_section');
}
Data Corruption:
data column in the database and repair:
$block = \App\Models\Block::find(1);
$block->data = json_decode($block->data, true);
$block->save();
Custom Block Types:
Block class for type-specific logic:
class HeroSection extends \Krafthaus\Bauhausblock\Block
{
public function getTitle()
{
return $this->data['title'] ?? 'Default Title';
}
}
'types' => [
'hero_section' => \App\Models\HeroSection::class,
],
Block Events:
\Krafthaus\Bauhausblock\Events\BlockCreated::class => function ($event) {
// Send notification or log creation
};
Query Scoping:
use Illuminate\Database\Eloquent\Scope;
class ActiveScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->whereJsonContains('data->active', true);
}
}
Block model:
protected static function booted()
{
static::addGlobalScope(new ActiveScope);
}
Block Storage:
Bauhausblock interface:
$this->app->bind(\Krafthaus\Bauhausblock\Contracts\BlockRepository::class, \App\Services\RedisBlockRepository::class);
How can I help you explore Laravel packages today?