codersfree/laratheme
Laratheme es un paquete para Laravel 11/12 que permite gestionar múltiples temas visuales. Cambia el tema activo por configuración, carga vistas con namespace automático y genera nuevos temas con php artisan make:theme. Incluye soporte para assets y stubs publicables.
Installation
composer require codersfree/laratheme
Publish the package configuration and migrations:
php artisan vendor:publish --provider="CodersFree\Laratheme\LarathemeServiceProvider" --tag="config"
php artisan vendor:publish --provider="CodersFree\Laratheme\LarathemeServiceProvider" --tag="migrations"
php artisan migrate
Basic Configuration
Edit config/laratheme.php to define:
resources/themes)Create Your First Theme
resources/themes/default) with:
views/ (Blade templates)assets/ (CSS/JS)config.php (theme-specific settings)First Use Case: Switching Themes
// In a controller or middleware
\CodersFree\Laratheme\Facades\Laratheme::setTheme('new-theme-name');
User-Based Theme Selection
// Middleware to set theme per user
public function handle($request, Closure $next) {
$theme = auth()->user()->theme ?? config('laratheme.default');
Laratheme::setTheme($theme);
return $next($request);
}
Dynamic Theme Switching via API
// API endpoint to change theme
Route::post('/theme/switch', function (Request $request) {
$request->validate(['theme' => 'required|exists:themes,name']);
Laratheme::setTheme($request->theme);
return response()->json(['success' => true]);
});
Theme-Specific Assets
// In Blade templates
<link href="{{ Laratheme::asset('css/style.css') }}" rel="stylesheet">
<script src="{{ Laratheme::asset('js/app.js') }}"></script>
Fallback Assets
// Use a fallback theme if the current theme lacks an asset
<link href="{{ Laratheme::asset('css/style.css', 'fallback-theme') }}" rel="stylesheet">
Theme-Specific Views
// Blade template will automatically look in `resources/themes/[current-theme]/views/`
@extends('layouts.app')
Partial Overrides
// Override a partial (e.g., `header.blade.php`) in a specific theme
@includeIf(Laratheme::viewExists('partials.header'), 'partials.header')
Theme-Specific Config
// Access theme config
$darkMode = Laratheme::config('dark_mode', false);
Merge Global + Theme Config
// In a service provider
$config = array_merge(
config('app'),
Laratheme::config()
);
Asset Paths
Laratheme::asset() instead of hardcoding paths. Verify theme_assets_path in config.View Caching
php artisan view:clear
Middleware Order
LarathemeMiddleware runs before ShareErrorsFromSession and StartSession in app/Http/Kernel.php.Theme Naming Conflicts
config, routes).theme_dark, theme_light).Check Active Theme
dd(Laratheme::getActiveTheme());
Verify Theme Exists
if (!Laratheme::themeExists('my-theme')) {
abort(404, 'Theme not found');
}
Log Theme-Specific Errors
try {
Laratheme::view('non-existent-file');
} catch (\Exception $e) {
\Log::error("Theme error in [{$theme}]: " . $e->getMessage());
}
Custom Theme Storage
Override the default theme storage (e.g., database, cache) by binding a custom ThemeRepository:
$this->app->bind(
\CodersFree\Laratheme\Contracts\ThemeRepository::class,
\App\Services\CustomThemeRepository::class
);
Theme Events Listen for theme-switch events:
Laratheme::onThemeSwitched(function ($oldTheme, $newTheme) {
\Log::info("Switched from {$oldTheme} to {$newTheme}");
});
Dynamic Theme Loading
Load themes from external sources (e.g., S3, API) by extending ThemeLoader:
class S3ThemeLoader extends \CodersFree\Laratheme\Services\ThemeLoader {
public function load($theme) {
// Custom logic to fetch theme from S3
}
}
How can I help you explore Laravel packages today?