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

v1.2.1 2025-09-19 06:28 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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 running php artisan theme:publish-vite

    This 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 run php artisan theme:install

    theme:install validates and (re)creates theme resources and the Vite setup and may overwrite generated files.

  1. Install the theme package (required first step):

    php artisan theme:install
  2. 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

๐Ÿ“œ License

  • License for more information. โš–๏ธ