Installation Add the bundle via Composer:
composer require brouzie/widgets-bundle
Register the bundle in config/app.php under providers:
Koc\BrouzieWidgetsBundle\BrouzieWidgetsBundle::class,
Publish Config & Assets Run:
php artisan vendor:publish --provider="Koc\BrouzieWidgetsBundle\BrouzieWidgetsBundle" --tag="config"
This generates a config/brouzie_widgets.php file. Update paths, widget storage, and caching settings as needed.
First Widget Creation
Use the make:widget Artisan command:
php artisan make:widget MyFirstWidget
This generates:
WidgetService class (handles logic).WidgetView (Blade template).WidgetConfig (for configurable options).Register the Widget
Add your widget to config/brouzie_widgets.php under widgets:
'widgets' => [
'my_first_widget' => [
'class' => \App\Widgets\MyFirstWidget::class,
'label' => 'My First Widget',
'configurable' => true, // If it has options
],
],
Render a Widget In a Blade template or controller:
{{ widget('my_first_widget', ['option1' => 'value1']) }}
Or programmatically:
$widget = app(\Koc\BrouzieWidgetsBundle\WidgetManager::class)->render('my_first_widget', ['option1' => 'value1']);
\Koc\BrouzieWidgetsBundle\AbstractWidget to define logic.
namespace App\Widgets;
use Koc\BrouzieWidgetsBundle\AbstractWidget;
class MyFirstWidget extends AbstractWidget {
public function getView(): string {
return 'widgets.my_first_widget'; // Blade path
}
public function configure(array $config): void {
$this->options = $config;
}
}
resources/views/widgets/my_first_widget.blade.php).
Access options via $widget->options.WidgetConfig to expose options in a CMS-like interface.
Example config:
protected function getConfigFields(): array {
return [
'title' => ['type' => 'text', 'label' => 'Widget Title'],
'color' => ['type' => 'select', 'options' => ['red', 'blue']],
];
}
{{ widget('my_first_widget', $pageConfig->widgetOptions) }}
config/brouzie_widgets.php:
'cache' => [
'enabled' => true,
'ttl' => 3600, // 1 hour
],
WidgetManager::clearCache('widget_name') after updates.WidgetRepository).themes/dark/widgets/my_first_widget.blade.php).$this->app->bind(
\Koc\BrouzieWidgetsBundle\WidgetRepository::class,
\App\Repositories\CustomWidgetRepository::class
);
WidgetRendering events to modify output globally.Widget Registration Order:
config/brouzie_widgets.php before first use. Lazy-loading isn’t supported.BootstrapServiceProvider).Blade Template Paths:
resources/views/widgets/. Custom paths require overriding getView() or configuring view_path in the config.'../widgets/my_widget') if organizing views differently.Circular Dependencies:
widget() inside a widget’s configure() method to prevent infinite loops.Configurable Widgets Without Options:
'configurable' => true but not defining getConfigFields() will throw errors.getConfigFields() or set 'configurable' => false.Database Storage Quirks:
widgets table exists and matches the bundle’s migrations (or extend WidgetRepository to handle custom schemas).dd(app(\Koc\BrouzieWidgetsBundle\WidgetManager::class)->getWidgets());
php artisan view:clear).config/brouzie_widgets.php for typos in widget class names or paths.Custom Storage:
Override WidgetRepository to store widgets in a NoSQL database or API.
class ApiWidgetRepository extends WidgetRepository {
public function find($name) {
return $this->fetchFromExternalApi($name);
}
}
Widget Events:
Dispatch events in AbstractWidget:
event(new WidgetRendering($this));
Listen globally in EventServiceProvider.
Dynamic Widget Loading:
Implement WidgetLoaderInterface to load widgets from a remote service or cache.
Security: Add middleware to validate widget configurations:
$widget->options = $this->sanitizeOptions($config);
WidgetManager::preRender() to cache output during low-traffic periods.How can I help you explore Laravel packages today?