Views
Table of contents
1. Overview Table of Contents
Widgets allow you to extend core views without modifying the core view files. They are small, reusable view blocks (like cards, graphs, or stats) that can be dynamically registered and rendered.
π Location
resources/views/widgets/
Making your own widget Create a new widget using the following command:
php console make:widget ${widget-name}
Organize your widgets by feature for clarity, for example:
resources/views/widgets/
βββ dashboard/
βββ revenueCard.php
βββ userGrowth.php
βββ activeSessions.php
β Common Use Cases
- Add dashboard cards for analytics, stats, or quick actions.
- Inject plugin-specific panels into existing admin pages.
- Keep customizations separate from core framework views.
2. βοΈ Registering Widgets Table of Contents
You can register widgets in your controller or service before rendering the view:
$this->addWidget(
'dashboard.cards', // Slot name
'dashboard.revenueCard', // Path to view file (without .php)
['revenue' => 10350] // Data passed to the widget
);
You can register multiple widgets in the same slot:
$this->addWidget('dashboard.cards', 'dashboard.userGrowth', ['users' => $users]);
$this->addWidget('dashboard.cards', 'dashboard.activeSessions', ['sessions' => $sessions]);
3. π¨ Rendering Widgets Table of Contents
Render all widgets in a slot:
Place this in your view where you want all registered widgets for a slot to appear:
<?= $this->renderWidgets('dashboard.cards', $this->widgets) ?>
Render a single widget manually:
If you want to directly include a specific widget without registering:
<?= $this->renderWidget('dashboard/activeSessions', ['sessions' => $sessions]) ?>
4. π Slots Table of Contents
A slot is simply a grouping name that organizes widgets in a particular location. Example slots:
dashboard.attachments
β for adding widgets to the admindashboard.attachment_details viewdashboard.details
β for widgets on the user profile (admindashboard.details) viewdashboard.index
β for widgets in the admindashboard.index viewdashboard.acls
β for widgets in the manage_acls view
5. π§ Best Practices Table of Contents
β
Keep widgets small and focused (e.g., one widget per feature)
β
Group widgets by folder (e.g., dashboard/
)
β
Pass only the data needed to keep them decoupled
β
Use addWidget()
for user-extensible areas, rather than editing core views
6. π Related Methods Table of Contents
Method | Description |
---|---|
addWidget($slot, $viewPath, $data) |
Registers a widget to a slot. |
renderWidgets($slot, $widgets) |
Renders all widgets in a slot. |
renderWidget($viewPath, $data) |
Renders a single widget file. |
7. β Example Table of Contents
Controller
$this->view->addWidget('dashboard.index', 'dashboard.activeSessions', $sessions);
Location
resources/views/widgets/
βββ dashboard/
βββ activeSessions.php
View Hook inside index view for AdminDashboard:
<div class="widget-container">
<?= $this->renderWidgets('dashboard.index', $this->widgets) ?>
</div>
<?php $this->end(); ?>
activeSessions.php Widget
<?php use App\Models\Users; ?>
<?php use Core\Lib\Utilities\DateTime; ?>
<h1 class="text-center mt-5">User Sessions</h1>
<table class="table table-striped table-bordered table-hover">
<thead>
<th>User Name</th>
<th>Created</th>
</thead>
<tbody>
<?php foreach($data as $session): ?>
<tr>
<td><?= Users::findById($session->user_id)->username ?></td>
<td><?= DateTime::timeAgo($session->created_at) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
By using widgets, you can build a modular, plugin-friendly dashboard that is easy to extend and maintain without touching the frameworkβs core views.