Skip to main content

Laravel Extension Showcase

Build three features with Laravel Extension. Each scenario shows what to scaffold, which code belongs in the extension, and how to verify the result in a Laravel application.

Before you start

Complete the installation guide and run the examples in a development application. The paths below assume Laravel's default App namespace. Each scenario is sample application code, so start with unused extension names.

Understanding Extension Components​

PieceWhat it doesExample
Extension typeOrganizes discoverable featuresModule or a host-defined Component
Extension class and manifestDescribe a feature and declare its resourcesBanner.php and extension.json
Plugin instanceStores configuration for one use of an installed extensionHomepage banner
CompositionLets one extension own the lifecycle of declared childrenShop owns LatestProducts

Reusable Banner Module​

Plugin instances

One implementation, two placements

A homepage and a campaign landing page need different messages. Share the banner code and store each message in its own plugin record.

1. Generate the module​

php artisan extension:make Module Banner --multiple --no-interaction

Keep the generated manifest next to Banner.php. Verify that its multiple flag is true. Replace the generated class with this implementation:

<?php

namespace App\Extensions\Module\Banner;

use JobMetric\CustomField\CustomFieldBuilder;
use JobMetric\Extension\Contracts\AbstractExtension;
use JobMetric\Extension\Kernel\ExtensionCore;
use JobMetric\Form\FormBuilder;

class Banner extends AbstractExtension
{
public function configuration(ExtensionCore $extension): void
{
$extension->name('banner');
}

public function form(): FormBuilder
{
return (new FormBuilder)->tab(function ($tab) {
$tab->id('content')
->label('Content')
->selected()
->group(function ($group) {
$group->label('Banner copy')
->customField(function (CustomFieldBuilder $field) {
$field::text()->name('headline')
->label('Headline')
->validation('required|string|max:120');
});
});
});
}

public function handle(array $options = []): ?string
{
return $options['headline'] ?? null;
}
}

2. Install and configure both placements​

php artisan extension:install Module Banner

Run this once from a trusted setup command or Laravel Tinker:

use JobMetric\Extension\Facades\Plugin;

$homepage = Plugin::add('Module', 'Banner', [
'name' => 'Homepage banner',
'status' => true,
'fields' => ['headline' => 'Welcome to our website'],
]);

$campaign = Plugin::add('Module', 'Banner', [
'name' => 'Campaign banner',
'status' => true,
'fields' => ['headline' => 'See what is new this season'],
]);

3. Render the selected instance​

Use the stored plugin ID for the placement. Your application decides which ID belongs on which page.

$headline = Plugin::run($pluginId);
@if ($headline !== null)
<aside aria-label="Announcement">{{ $headline }}</aside>
@endif

Verify the result: The two instances return different headlines. A blank headline fails validation when creating an instance. A disabled instance returns null, so the announcement is omitted.

Optional Reporting Feature​

Routes and authorization

Install an internal report only where it is needed

Ship the route with the module. Keep access decisions in the host application's authorization layer.

1. Scaffold the reporting module​

php artisan extension:make Module Reporting --route --no-interaction

Keep the generated manifest and class. Ensure the class's configuration method registers the route file:

public function configuration(ExtensionCore $extension): void
{
$extension->name('reporting')->hasRoute();
}

2. Define a protected route​

In app/Extensions/Module/Reporting/routes/route.php:

<?php

use Illuminate\Support\Facades\Route;

Route::middleware(['web', 'auth', 'can:view-reports'])
->get('/internal/reports/summary', function () {
return response()->json(['report' => 'summary']);
})
->name('internal.reports.summary');

Register the view-reports ability in the host application using the permissions appropriate to your users. The sample response is a stub; replace it with the data your report should return.

3. Install and inspect​

php artisan extension:install Module Reporting
php artisan route:list --name=internal.reports.summary

Verify the result: The route appears after installation. An authenticated user with the ability receives the summary response; an authenticated user without it receives 403. Refresh the Laravel route cache after changing installed extensions in deployments that cache routes.

Component with Child Extensions​

Composition

Ship a feature and its supporting modules together

A Shop component can own a LatestProducts module. Both have their own class and manifest, and installation is coordinated through the Shop root.

1. Register the types​

Run this in a host service provider's register method before kernel discovery. Module is already a default type; ensure it remains registered in your configuration.

use JobMetric\Extension\Facades\ExtensionTypeRegistry;

ExtensionTypeRegistry::register('Component', [
'contains' => ['Module'],
]);

2. Declare the manifest and filesystem layout​

In Shop's extension.json, use the generated metadata and add the children descriptor:

{
"extension": "Component",
"name": "Shop",
"title": "Shop",
"version": "1.0.0",
"multiple": false,
"priority": 0,
"depends": [],
"children": [
{"type": "Module", "name": "LatestProducts"}
]
}
app/Extensions/Component/Shop/
├── Shop.php
├── extension.json
└── Extensions/Module/LatestProducts/
├── LatestProducts.php
└── extension.json

The child manifest uses Module and LatestProducts as its type and name. Both classes implement the extension contract. The child class must use the nested namespace App\Extensions\Component\Shop\Extensions\Module\LatestProducts.

3. Install the parent and resolve the child​

php artisan extension:install Component Shop
use App\Extensions\Component\Shop\Shop;
use JobMetric\Extension\Facades\Extension;

$child = Extension::getInfo('Module', 'LatestProducts', false, Shop::class);

Verify the result: The child is installed through Shop and can be looked up using the owner's namespace. A direct attempt to install the child is rejected. Review the composition guide before adding migrations, dependencies, or upgrades.

Get Started​

Install Laravel Extension, then use the Plugin Service reference to integrate plugin configuration and rendering into your application.