First, install new Laravel application to be used as testing application. We are not using DB, so It might be good enough to start new application with Herd. The folder structure after initiating the application will look like
my-laravel-app/
└── packages/
└── your-vendor/
└── blade-theme/
├── composer.json
├── src/
│ ├── BladeThemeServiceProvider.php
├── resources/
│ └── views/
│ └── layout.blade.php
├── routes/
│ └── web.php (optional, for demo/testing)
└── README.md
It is developed under “packages” directory. Then, it will be installed with Composer in product and the directory will be “/vendor/your-vendor/blade-theme/”. It will have the same level of directory structure.
composer.json (for your package)
{
"name": "your-vendor/blade-theme",
"description": "A Blade theme package for Laravel.",
"type": "library",
"autoload": {
"psr-4": {
"YourVendor\\BladeTheme\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"YourVendor\\BladeTheme\\BladeThemeServiceProvider"
]
}
},
"require": {
"php": "^8.1",
"illuminate/support": "^10.0 || ^11.0 || ^12.0"
}
}
BladeThemeServiceProvider.php
<?php
namespace YourVendor\BladeTheme;
use Illuminate\Support\ServiceProvider;
class BladeThemeServiceProvider extends ServiceProvider
{
public function boot()
{
// Load package views
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'blade-theme');
// Optionally publish views for customization
$this->publishes([
__DIR__ . '/../resources/views' => resource_path('views/vendor/blade-theme'),
], 'blade-theme-views');
}
public function register()
{
//
}
}
Example View (layout.blade.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>@yield('title', 'Blade Theme')</title>
</head>
<body>
<header>
<h1>Welcome to My Blade Theme</h1>
</header>
<main>
@yield('content')
</main>
<footer>
<p>Powered by Blade Theme</p>
</footer>
</body>
</html>
Add the Package to Laravel (my-laravel-app/composer.json)
"repositories": [
{
"type": "path",
"url": "packages/your-vendor/blade-theme"
}
]
Then you can run Composer to require your package.
composer require your-vendor/blade-theme:@dev
When all of them are done, you can test the Theme in your app. Create a Blade file using your package layout:
{{-- resources/views/welcome.blade.php --}}
@extends('blade-theme::layout')
@section('title', 'My Custom Page')
@section('content')
<p>This is using the Blade theme!</p>
@endsection
And route it:
Route::get('/', function () {
return view('welcome');
});
Some tips for trouble shooting can be:
Checking your Laravel (illuminate) version.
php artisan --version
Restarting and Rebuilding after changing namespace or structure
php artisan view:clear
php artisan config:clear
composer dump-autoload
Publishing the views
php artisan vendor:publish --tag=cemclf-views
I have built one Composer package for my office which mimics Drupal site of https://cemc.uwaterloo.ca. The repository is available on: https://github.com/waterloobae/cemclf
Composer package on Packagist is https://packagist.org/packages/waterloobae/cemclf