Views

Table of contents

  1. Overview
  2. Registering Widgets
  3. Rendering Widgets
  4. Slots
  5. Best Practices
  6. Related Methods


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 view
  • dashboard.details – for widgets on the user profile (admindashboard.details) view
  • dashboard.index – for widgets in the admindashboard.index view
  • dashboard.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


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.