What are the best practices for creating a “settings” model in Laravel 5?

你离开我真会死。 提交于 2021-02-07 18:15:09

问题


This is my first project using Laravel (I'm starting with 5.2). I'm building a self managed website. There's an admin that should save site settings for using later in the website. Eg: Background color, social networks, carousel with pictures, products, etc.

My first try was creating a model "Setting" with: id | key (unique) | value. When I tried to save social networks (to display in the site header), I realized I'd have to save in "value" a json with URL, title, etc... Ugly implementation for a relational model, so I came to the conclussion it was bad. I created a sepparated model SocialNetwork with: id | URL | image | name.

Now I'm thinking in loading all these models at once in my BaseController so I can have it through all controllers, but I'm not sure it's correct. I mean, an array $settings with all models as part of it:

abstract class BaseController extends Controller
{
    protected $settings;
    ...

    public function __construct(...)
    {
        $this->settings = [
            'backgroundColor' => 'red',
            'socialNetworks' => SocialNetwork::all(),
            'other' => Other::all(),
            ...
        ]
    }
}

It looks horrible to me, but I'm lost with this framework and I don't know what's the best I could do to deal with this. Someone told me about service providers, but I'm trying to figure out the advantages of it and the best approach to be able to also made use of these models (to save data) and I don't get it.

I'm not asking for explicit code (it would be nice however), I'll investigate it after understanding the approach, but some tips or conceptual help would be really appreciated.

Thank you!


回答1:


This is a great question. Recently i did for one of my Code Canyon product (Krisma CMS Script) and my approach was as below.

1) Created a Service Provider

public function boot()
    {
        $this->header();
        $this->intro();
        $this->about();
        $this->resume();
        $this->portfolio();
        $this->services();
        $this->stats();
        $this->contact();
        $this->footer();
        $this->section();
        $this->codesetting();
        $this->customizer();
    }

Each of this function which is called in the boot is declared in the Provider Class below boot function. e.g for Header Section i fetched data from DB via Header Model and shared it globally which can be accessed any where in your app.

$section = Section ::find(1);
view()->share('section ', $section);

2) Created a table for each section i.e about, intro etc and inserted the values which i want to modify.

3) Now in the admin panel create a form for changes/modifications and update the desired section properties.

4) Fetch the data in your view. Suppose if the user has changed the background color from admin panel to RED, so print out that variable right in your view style : {{ $sectionname->color }}

5) That's it i hope it gives you a better idea of how settings can be implemented and i did this for my Product, may be there is a better way but up to now this is what i found in my research and works perfect for me.



来源:https://stackoverflow.com/questions/39111507/what-are-the-best-practices-for-creating-a-settings-model-in-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!