Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Felix's avatar

Implement mutiple themes in a Laravel CMS

Hi, like the topic says, is there a nice and good way to use different Designs/Themes? I build a custom CMS and I need a way to create a new theme if I want to use it for a new site.

Felix

0 likes
16 replies
foxted's avatar

I never really tackled this problem, but my first idea goes to a theme name in a config file (or database) and each theme has its folder in the views directory, as well as the assets, so you can dynamically change the path to your views and assets.

You'll probably need to find a way to organise your code and try to reduce the number of views you have (maybe create some convention for views like WordPress for example).

mstnorris's avatar

By themes do you mean changing the layout? If so, this should be done purely in css. The data stays the same but the stylesheets control the layout. All you'd need to do then is as @foxted says is to point to a css file from your config file.

2 likes
Felix's avatar

Its not just the CSS ;/. And yes like wordpress. So that I can change the complete layout if I want, with menus, sidebars, header etc.

1 like
eminiarts's avatar

A config file and multiple layouts files (as an easy option...)?

This (paid) package is awesome for anything more: https://cartalyst.com/manual/themes

Though it's a paid package, you can get the idea from its documentation.

eokorie's avatar

I have actually been using this package for my themeing - https://github.com/teepluss/laravel4-theme. Seems to work pretty well.

1 like
mrterryh's avatar

It shouldn't be too difficult. Have a directory in the root named themes like so:

app
tests
...
themes
    bootstrap
        ...
        views
            layout.blade.php
    default
        ...
            views
                layout.blade.php

Then, you could have a config option to determine which theme to use.

<?php

// app/config/site.php

return [
    'theme' => 'default'
];

Finally, you can register the path to the theme, referencing the config value you just created:

$theme = Config::get('site.theme');

View::addLocation(base_path() . '/themes/' . $theme . '/views/');

As always, there are many ways to do this. There's almost certainly a better way than this. It's just a suggestion to get you started though :)

8 likes
TravisBlasingame's avatar

The way I have tackled this is with a theme config containing two options.

    'theme' => [
        'style' => 'dark',
        'src' => 'vendor',
    ],

I have a package that I use to store my themes that I create for a site. These generally contain a few folders.

themes
    - assets
        - less
            - default
            - dark
        - js
    - views

For the assets I use gulp when creating a new site to build the css and js. If the config is set to vendor, it uses these less files and thats all. If i set the config to local, it uses my local changes to the less files (which generally extend the vendor ones). The theme option in the config tells it which folder to look in.

For the views, you can do this one of two ways. First, in a service provider, register the new view like @mrterryh mentioned.

    /**
     * Register views
     *
     * @return void
     */
    protected function registerViews()
    {
        $this->app['view']->addLocation(__DIR__ . '/../../views');
    }

This is the method I call in my service providers register method. The second way is in Laravel's built in view.php config.

 /*
 |--------------------------------------------------------------------------
 | View Storage Paths
 |--------------------------------------------------------------------------
 |
 | Most templating systems load templates from disk. Here you may specify
 | an array of paths that should be checked for your views. Of course
 | the usual Laravel view path has already been registered for you.
 |
 */

 'paths' => [__DIR__.'/../views'],

This is an array of locations to look for views and it goes in order. Either way you do this, laravel will look in app/views first. If it does not find the view, it will look in each other registered location based on when it was registered (this allows you to control the order when using the service provider version by just changing the order you add them to app.php config).

Hope this helps. It's how I have been doing it and it adds the ability to create a set of core layouts/views and assets and easily overload them for the needs of a specific site.

2 likes
jakeryansmith's avatar

So what would be the best way to allow a user to switch to a new theme? Would you just save the theme path in some kind of settings table and then run a short query before rendering each view?

igaster's avatar

Changing the views path: solved.

But what about assets? How can we relocate the 'public' folder to eg 'themes\DarkTheme'

This would solve the problem digging into html (and css) and updating all links....

1 like
bashy's avatar

You would have to get the theme name globally with a variable I guess.

NoorDeen's avatar

for the user you can save the them name in cookies and fetch the them from it if there no cookie fall-back to the default theme

igaster's avatar

Hello! I've created a theme package for laravel 5 with features like:

  • Views & Asset seperation in theme folders
  • Theme inheritence: Extend any theme and create Theme hierarcies

Try it here: igaster/laravel-theme

6 likes
Aleem's avatar

Hi I am new in laravel for last 1 week. I have an assignment to develop an web application in which user select theme which he/she like. I have two different theme in laravel application now i want to switch one theme to another theme during runtime. I waste two days for this small task and i tried lot of thing like laravel/igastertheme package of you and follow it step by step but could not found success and i do not find any video link or tutorial to managing multiple theme in an application. I see you(igaster) in different threads or your package name. I request you please made a video how to use your package, add multiple themes and switch between these theme during run time its help me and beginner like me. i am very grateful to you.

1 like
Tetravalence's avatar

@Aleem raised the problem I'm facing with package development. Does anyone know if I have to use a Middleware class to filter any asset used in the active theme? For exemple if current theme does use assets from this directory

/vendor/tetravalence/package/template/current/public/assets/css/style.css

the Middleware should copy style.css into public but I'm not sure if this task can be accomplished in an efficient way. I reject the solution about using artisan command because the user has to active the theme on his own initiative. I would be grateful for any advice you can give me.

Please or to participate in this conversation.