简体   繁体   中英

How to make a custom theme using Laravel and where to store theme data?

I'm building a full website Using Laravel and this problem is facing me , I want to give the user full control to the site appearance and the ability to change website's layout without my help.

But when I'm thinking about someway to do so by database , there are several tables with tens of columns will be added beside the colors and sections that will be added to database every time the user will change something in the layout style.

Is there any other way to store the options of the theme in XML file or anything other than database?

** Note : when I checked the database of Wordpress I hadn't found any thing concerned to themes there , So where Wordpress store theme options?

For your XML-like storage you can use Laravel Config. Let's say you have the file config/templates.php . Inside you can have a lot of stuff like.

return [
    'default_template' => 'layouts.templates.default',
    'posts' => 'layouts.templates.post',
    'footer' => 'layouts.includes.footer',

    'options' => [
        'full_with' => false,
        'has_footer' => true,
        'background_color' => '#fff',
        'more_keys' => 'key_value'
    ]
];

Then anywhere in your app you can get the default template like

$view = config('templates.default_template'); //will get
return view($view, compact('params'));

To update a key, like has_footer , you do

config(['templates.options.has_footer' => false]); //will update

This should get you started I think

Simple Example

Let's say user changes default colour from and input and submit the form. You do

public function updateTheme(Request $request) {
    if ( $request->has('background_colour') && $request->background_colour != '' ) {
        config(['templates.options.background_colour' => $request->background_colour]);       
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM