创建“设置"的最佳做法是什么?Laravel 5中的模型? [英] What are the best practices for creating a "settings" model in Laravel 5?

查看:40
本文介绍了创建“设置"的最佳做法是什么?Laravel 5中的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用Laravel的第一个项目(我从5.2开始).我正在建立一个自我管理的网站.有一个管理员应保存网站设置,以供以后在网站中使用.例如:背景色,社交网络,带有图片的旋转木马,产品等.

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.

我的第一个尝试是使用 id |创建模型"Setting".钥匙(唯一)|值.当我尝试保存社交网络(以显示在网站标题中)时,我意识到我必须将带有URL,标题等的json保存在值"中.结论很糟糕.我创建了一个单独的模型SocialNetwork,其代码为: id |网址|图片名称.

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.

现在我正在考虑一次将所有这些模型加载到我的BaseController中,这样我就可以在所有控制器中使用它,但是我不确定它是正确的.我的意思是,一个数组 $ settings ,其中包含所有模型:

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.

谢谢!

推荐答案

这是一个很好的问题.最近,我为我的Code Canyon产品之一( Krisma CMS脚本)做过方法如下.

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)创建了一个服务提供商

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)为每个部分(例如简介",简介"等)创建一个表格,并插入要修改的值.

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

3)现在,在管理面板中创建一个用于更改/修改的表单,并更新所需的部分属性.

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

4)获取您视图中的数据.假设用户将背景颜色从管理面板更改为红色,那么请在您的视图 style:{{$ sectionname-> color}}

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)就是这样,我希望它可以使您更好地了解如何实现设置,而我为我的产品做到了这一点,也许有更好的方法,但是到目前为止,这是我在研究和工作中发现的对我来说很完美.

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.

这篇关于创建“设置"的最佳做法是什么?Laravel 5中的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆