rdcstarr / laravel-themes
A simple themes package for Laravel.
Installs: 30
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/rdcstarr/laravel-themes
Requires
- php: ^8.0
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
A powerful and flexible Laravel package for theme management with seamless Vite integration.
โจ Features
- ๐จ Easy Theme Management - Add, list, and remove themes with simple Artisan commands ๐ ๏ธ
- โก Vite Integration - Full Vite support with hot reload and optimized builds โก๏ธ๐ฅ
- ๐ Inline Asset Rendering - Render CSS/JS assets inline for performance optimization ๐
- ๐ Flexible Directory Structure - Customize theme directories via configuration ๐๏ธ
- ๐ฏ Global Helper Functions - Access themes anywhere with the
theme()helper ๐งฉ - ๐งฉ Blade Directives - Convenient Blade directives for theme integration โจ
- ๐ Redis Caching - Optimized performance with Redis caching support โก๏ธ๐ง
- ๐ฆ Auto-Discovery - Automatic service provider registration โ
๐ฆ Installation
Install the package via Composer:
composer require rdcstarr/laravel-themes
โ๏ธ Required Setup
๐ก Important: The Vite configuration is regenerated depending on which directories you change in
config/themes.php.
If you only change
themes.directories.build(the public/build path), you can update the Vite configuration without reinstalling themes by runningphp artisan theme:publish-viteThis updates Vite entries and build paths only.
If you change
themes.directories.resources(the resources/themes path) โ for example where theme views, CSS/JS and images live โ you must runphp artisan theme:install
theme:installvalidates and (re)creates theme resources and the Vite setup and may overwrite generated files.
-
Install the theme package (required first step):
php artisan theme:install
-
Publish the configuration file (optional):
php artisan vendor:publish --tag=theme-config
๐ ๏ธ Artisan Commands
๐ Install Theme Package (required first):
php artisan theme:install [--force] [--manifest] [--tailwind]
Sets up the default theme and publishes Vite configuration files.
๐จ Add a New Theme:
php artisan theme:add {theme-name} [--manifest] [--tailwind]
Creates a complete theme structure with CSS, JS, views, and images directories. Use --manifest to also create a manifest.json file.
๐ List Available Themes:
php artisan theme:list
Shows all available themes with their paths.
๐๏ธ Remove a Theme:
php artisan theme:remove {theme-name} [--force]
Deletes the specified theme. Use --force to skip confirmation.
๐ Create/Recreate Theme Manifest:
php artisan theme:manifest-publish {theme-name} [--force]
Creates or recreates a manifest.json file for an existing theme. Use --force to skip confirmation when overwriting.
๐ Manage Theme Manifest Fields:
# List all fields in a theme's manifest php artisan theme:manifest {theme-name} list # Add a new field interactively php artisan theme:manifest {theme-name} add # Add a field with specific parameters php artisan theme:manifest {theme-name} add --key=about.title --label="About Title" --type=text # Remove a field php artisan theme:manifest {theme-name} remove [--key=field-key]
Manage custom fields in theme manifest files for dynamic content configuration.
โก Publish Vite Configuration:
php artisan theme:publish-vite [--force]
Updates the Vite configuration file with theme support.
๐ฏ Theme API
Basic Operations:
// Get the current theme instance ๐งญ theme() // Set the current theme ๐๏ธ theme()->set('theme-name'); // Get the current theme name ๐ท๏ธ theme()->name(); // Check if a theme exists โ theme()->exists('theme-name'); // Get all available themes ๐ theme()->getAll();
Path Helpers:
theme()->basePath(); // Base path for all themes theme()->path(); // Current theme path theme()->viewsPath(); // Views directory theme()->jsPath(); // JavaScript file path theme()->cssPath(); // CSS file path
Vite Integration:
theme()->viteJs(); // Vite JS entry point theme()->viteCss(); // Vite CSS entry point theme()->viteImages(); // Vite images directory theme()->getBuildDirectoryPath(); // Build output directory theme()->getHotFile(); // Hot reload file
๐จ Blade Directives
Theme Information:
{{-- Display current theme name --}} @themeName
Asset Integration:
{{-- Standard Vite assets (requires Vite dev server or build) --}} @vite(theme()->viteCss()) @vite(theme()->viteJs()) {{-- Standard Vite assets (requires Vite dev server or build) --}} @viteCss // with auto path to theme()->viteCss() @viteJs // with auto path to theme()->viteJs() {{-- Inline assets (injects CSS/JS directly into HTML) --}} @viteCssInline @viteJsInline {{-- Custom inline assets --}} @viteInline([theme()->viteCss(), theme()->viteJs()])
Images with Vite:
{{-- In your Blade templates --}} <img src="{{ Vite::image('logo.png') }}" alt="Logo">
โ๏ธ Configuration
The config/themes.php file allows you to customize directories:
return [ 'default' => env('THEME_DEFAULT', 'default'), 'directories' => [ 'resources' => 'themes', // complete path will be `./resources/themes` 'build' => 'themes', // complete path will be `./public/themes` ], ];
๐ฅ Vite Development
๐ Start development server for a specific theme:
npm run dev --theme=my-theme
๐ข Build assets for production:
npm run build --theme=my-theme
Vite Configuration Features:
- โ Theme-specific hot reload files
- โ Automatic theme directory validation
- โ Asset path resolution
- โ Full page reload on view changes
๐ Theme Structure
Each theme follows this organized structure:
resources/themes/my-theme/
โโโ css/
โ โโโ app.css # Main CSS file
โโโ js/
โ โโโ app.js # Main JavaScript file
โโโ images/ # Theme images
โโโ views/ # Blade templates
โโโ welcome.blade.php
๐ Middleware Integration:
// Set theme based on your preferences public function handle($request, Closure $next) { theme()->set('theme_name' ?? 'default'); return $next($request); }
โก Performance Features
Redis Caching:
- โ Automatic caching when Redis is configured ๐ง
- โ Theme existence checks cached for 30 seconds โฑ๏ธ
- โ Manifest files cached for 30 seconds ๐ฆ
- โ Asset content cached for 30 seconds ๐พ
Inline Asset Benefits:
- โ Reduces HTTP requests ๐
- โ Eliminates render-blocking resources ๐ซ๐งฑ
- โ Perfect for critical CSS/JS ๐
- โ Automatic caching in production โ
๐งช Testing
composer test
๐ Resources
- Changelog for more information on what has changed recently. โ๏ธ
๐ฅ Credits
- Rdcstarr ๐
๐ License
- License for more information. โ๏ธ