elao/theme-bundle
ElaoThemeBundle is a Symfony bundle (work in progress) intended to provide theme support for applications. The repository currently contains minimal documentation and may be incomplete or unstable.
Installation
Add the bundle to your composer.json under require:
composer require elao/theme-bundle
Enable it in config/bundles.php (Symfony 4+):
Elao\ThemeBundle\ElaoThemeBundle::class => ['all' => true],
Configuration Publish the default config:
php artisan vendor:publish --tag=elao-theme-config
Edit config/elao_theme.php to define:
themes_path: 'themes')First Use Case: Basic Theme Switching
themes/ (e.g., themes/default, themes/alternative).config.yml (Symfony 2.x style) or config.php (Laravel-style) for theme-specific settings.elao_theme Twig extension to render theme-aware content:
{{ render_theme('homepage') }} {# Renders the homepage template from the active theme #}
themes/{theme_name}/routing.yml (Symfony 2.x) or routes/{theme}.php (Laravel).elao_theme service to dynamically load routes:
$theme = $this->get('elao_theme');
$theme->setActiveTheme('alternative');
elao_theme Twig functions:
{% stylesheets
'themes/*/css/style.css'
filter='cssrewrite'
%}{{ asset_url }}{% endstylesheets %}
?v={{ theme.version }} to asset URLs for theme-specific versioning.base.html.twig in themes/default/templates/).{# themes/alternative/templates/base.html.twig #}
{% block title %}Alternative Theme{% endblock %}
ThemeSwitcher controller (if enabled in config) to let users toggle themes via a frontend UI.$this->get('elao_theme')->setActiveTheme('user_selected_theme');
# themes/alternative/config.yml
theme:
color_scheme: dark
{{ theme_config.color_scheme }}
AppServiceProvider:
$this->app->bind('elao_theme', function ($app) {
return new \Elao\ThemeBundle\Service\ThemeManager($app['config']['elao_theme']);
});
public function handle($request, Closure $next) {
$theme = $request->user()->preferred_theme ?? config('elao_theme.default_theme');
app('elao_theme')->setActiveTheme($theme);
return $next($request);
}
Deprecated Symfony 2.x Bundle
Twig_Extension).ContainerAware classes need Laravel equivalents (e.g., ContainerInterface → Illuminate\Container\Container).Symfony\Component\DependencyInjection with Laravel’s DI).No Laravel-Specific Docs
Twig_Environment and Router. Override the ThemeManager to use Laravel’s View and Route services:
$viewFactory = app('view.factory');
$router = app('router');
Theme Caching Issues
php artisan view:clear
php artisan cache:clear
Asset Path Conflicts
mix or asset() helper to avoid conflicts:
<link href="{{ asset('themes/' ~ theme.name ~ '/css/style.css') }}" rel="stylesheet">
No Built-in Theme Preview
ThemeSwitcher is basic. Extend it with Laravel’s Session or Cookie to persist user selections:
$request->session()->put('theme', $themeName);
Check Active Theme Add a debug Twig function:
// In a custom Twig extension
public function getActiveTheme() {
return app('elao_theme')->getActiveTheme();
}
Use in templates:
{{ dump(active_theme()) }}
Verify Theme Directories
Ensure themes_path in config/elao_theme.php points to a valid directory:
'themes_path' => resource_path('themes'),
Template Not Found?
View resolver may not auto-discover themes. Manually register theme paths:
$viewPaths = [
resource_path('themes/' . $themeName . '/views'),
resource_path('views'),
];
$viewFactory->addLocation($viewPaths);
Config Overrides
Theme configs (e.g., config.yml) are merged with global configs. Use !important in YAML to force overrides:
theme:
setting: value !important
Custom Theme Loader
Extend Elao\ThemeBundle\Loader\ThemeLoader to support dynamic theme loading (e.g., from a database):
class DatabaseThemeLoader extends ThemeLoader {
public function load($themeName) {
$theme = DB::table('themes')->where('name', $themeName)->first();
return $theme ? $theme->path : null;
}
}
Theme Events
Add Laravel events for theme switches (e.g., theme.switched):
Event::listen('theme.switched', function ($theme) {
Log::info("Switched to theme: {$theme->getName()}");
});
Laravel Mix Integration Use Laravel Mix to compile theme assets:
// mix.js
mix.setPublicPath('themes/' + themeName + '/dist');
mix.js('themes/' + themeName + '/src/js/app.js', 'js');
Multi-Tenant Themes
Combine with Laravel’s tenancy packages to scope themes per tenant:
$theme = app('elao_theme')->setActiveTheme(Tenant::current()->theme);
How can I help you explore Laravel packages today?