Installation Add the package via Composer in your Laravel project:
composer require cvepdb-cms/module-themes
Publish the package configuration (if available):
php artisan vendor:publish --provider="Cvepdb\Themes\ThemesServiceProvider"
Service Provider & Routes
Ensure the package’s service provider is registered in config/app.php under providers.
Check for theme-related routes in routes/web.php (if the package includes them).
First Use Case: Activating a Theme
themes directory in your project (default: resources/themes or storage/app/themes).my-theme) with required files (index.blade.php, assets/).themes table or config).Configuration
Review config/themes.php (if published) for:
cache_themes: true).Standardized Directory Layout
Themes should follow a consistent structure (e.g., views/, assets/css/, assets/js/).
Example:
/resources/themes/my-theme/
├── index.blade.php # Main template
├── layouts/app.blade.php # Shared layout
├── assets/
│ ├── css/style.css
│ └── js/script.js
└── config.php # Theme-specific config (optional)
Blade Templates
Use @include and @extends for modularity. Example:
@extends('themes.my-theme.layouts.app')
@section('content')
<h1>Welcome to My Theme</h1>
@endsection
Middleware for Theme Detection Use middleware to detect and apply themes based on:
admin vs. user)./theme/{name}).namespace App\Http\Middleware;
use Cvepdb\Themes\Facades\Theme;
class SetTheme
{
public function handle($request, Closure $next) {
$theme = Theme::getActiveTheme(); // Assume facade exists
view()->share('theme', $theme);
return $next($request);
}
}
Theme Facade/Helper Leverage the package’s facade (if available) to:
$themes = Theme::listThemes();
Theme::setActiveTheme('my-theme');
if (Theme::isActive('my-theme')) { ... }
Versioned Assets Use Laravel Mix/Vite to compile theme assets with unique hashes:
// resources/themes/my-theme/assets/js/app.js
require('./theme.js');
Compile with:
npm run dev
Output will be hashed (e.g., css/style.css?id=1234).
Theme-Specific Asset Loading Dynamically load assets in Blade:
<link href="{{ asset("themes/{$theme}/assets/css/style.css") }}" rel="stylesheet">
Override Defaults
Publish and modify config/themes.php to:
'default' => 'my-theme'.'cache_themes' => false.'theme_paths' => [resource_path('themes'), storage_path('app/themes')].Theme-Specific Config
Store theme-specific settings in config.php within the theme directory and load them via:
$themeConfig = require(resource_path("themes/{$theme}/config.php"));
Service Provider Binding
Bind the theme resolver in AppServiceProvider:
public function boot() {
view()->composer('*', function ($view) {
$view->with('activeTheme', Theme::getActiveTheme());
});
}
Dynamic View Resolution Override Laravel’s view resolver to prioritize themes:
// app/Providers/ViewServiceProvider
public function boot() {
$this->app['view']->addNamespace('themes', resource_path('themes'));
}
Then use:
@include('themes.' . $activeTheme . '.index')
Theme Management UI If the package includes an admin panel:
/admin/themes) to:
Custom Admin Routes Add routes for theme management:
Route::prefix('admin/themes')->group(function () {
Route::get('/', [ThemeController::class, 'index']);
Route::post('/activate', [ThemeController::class, 'activate']);
});
Missing Theme Directories
resources/themes or storage/app/themes, but they’re missing.mkdir -p resources/themes
chmod -R 755 resources/themes
Caching Conflicts
php artisan view:clear
php artisan config:clear
config/themes.php temporarily for debugging:
'cache_themes' => false,
Asset Path Errors
asset() or mix():
<link href="{{ mix("themes/{$theme}/assets/css/style.css") }}" rel="stylesheet">
public_path() is correctly configured in config/filesystems.php.Namespace Collisions
resources/views/home.blade.php vs. resources/themes/my-theme/home.blade.php).@include('themes.' . $activeTheme . '.home')
Database Dependencies
themes table, but migrations aren’t run.database/migrations/ and run:
php artisan migrate
Middleware Order
SetTheme middleware is registered before other middleware in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\SetTheme::class,
// Other middleware...
];
Log Active Theme Add a debug line in middleware:
\Log::debug('Active theme:', ['theme' => $theme]);
Check logs with:
tail -f storage/logs/laravel.log
Verify Theme Paths Dump theme paths in a route:
Route::get('/debug-themes', function () {
dd(config('themes.theme_paths'));
});
Test with a Minimal Theme Create a barebones theme to isolate issues:
/resources/themes/test-theme/
└── index.blade.php
@extends('layouts.app')
@section('content') <h1>Test Theme</h1> @endsection
Custom Theme Resolver Extend the theme resolution logic by binding a custom resolver:
// app/Providers/AppServiceProvider
public function register() {
$this->app->bind(\Cvepdb\Themes\Contracts\ThemeResolver::class, function () {
return new \App\Services\CustomThemeResolver();
});
}
Theme Events Listen for theme-related events (if the package supports them):
// app/Providers/EventServiceProvider
protected $listen = [
\Cvepdb\Themes\Events\ThemeActivated::
How can I help you explore Laravel packages today?