isaeken/laravel-theme-system

Theme system for Laravel

v2.4 2021-10-11 13:20 UTC

This package is auto-updated.

Last update: 2024-04-14 10:50:39 UTC


README

Laravel Theme System

Latest Version CircleCI GitHub Code Style Action Status Total Downloads

Installation and setup

Installation

You can install the package via composer:

composer require isaeken/laravel-theme-system

Setup

You can publish the config file with:

php artisan vendor:publish --provider="IsaEken\ThemeSystem\ThemeSystemServiceProvider" --tag="theme-system-config" --tag="theme-system-migrations"
php artisan migrate

Run the following command in the terminal for initializing:

php artisan themes:init

Usage

Change the theme in runtime

theme_system()->setTheme('your-theme-name');

Get current theme name

theme_system()->getCurrentTheme();

Set theme per user

// \App\Models\User.php
class User extends Authenticatable
{
    use \IsaEken\ThemeSystem\Traits\CanChooseTheme; // Add this
    
    // ...
}
// In your controller or middleware
auth()->user()->theme; // theme for user.
auth()->user()->theme = 'default'; // theme is saved to db.

auth()->user()->themeApply(); // changed current theme to user theme.

Creating theme

Run the following command in the terminal.

php artisan make:theme your-theme-name

Change theme in PHP or application config.

Webpack

Do not change the main webpack.mix.js file.

A special "webpack.mix.js" file is created for each theme.

The "webpack.mix.js" file of the default theme is in the "resources" folder.

You can continue to use the "webpack.mix.js" as normal in the default theme.

However, in themes you should use it as in the example.

const mix = require('laravel-mix');

mix
    .js(themeResourceRoot + '/js/app.js', 'js')
    .postCss(themeResourceRoot + '/css/app.css', 'css', [
        //
    ]);

exports.mix = mix;

Middleware to set a theme

Register ThemeMiddleware in app\Http\Kernel.php:

protected $routeMiddleware = [
    // ...
    'theme' => \IsaEken\ThemeSystem\Http\Middlewares\ThemeMiddleware::class,
];

Example usages:

Route::group(['middleware' => 'theme:your-theme-name'], function () {
    // ...
});

Route::get('/hello-world', fn () => 'Hello World!')->middleware('theme:your-theme-name');

If you need to advanced methods, see ThemeSystem class.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.