简体   繁体   English

fuelPHP从数据库读取配置

[英]fuelPHP read config from db

I have table in DB with global settings for site. 我在数据库表中具有站点的全局设置。 I want to load these parameters when fuelPHP is starting and use it in all controllers. 我想在fuelPHP启动时加载这些参数,并在所有控制器中使用它。 table columns is id|setting_name|setting_value. 表列是id | setting_name | setting_value。 I used codeigniter and it was coded as helper. 我使用了codeigniter,它被编码为帮助程序。 I called function site_settings('parameter') and i got result. 我调用了函数site_settings('parameter')并得到结果。 How to do this in fuel? 如何用燃油做到这一点?

You would need to build a class to get it to work with Fuel. 您将需要建立一个类来使其与Fuel一起使用。

Creating a model might make sense since it's got built in hooks to a database. 创建模型可能是有道理的,因为它是内置在数据库的挂钩中的。 Check out Model_Crud or ORM. 签出Model_Crud或ORM。

http://fuelphp.com/docs/classes/model_crud/introduction.html http://fuelphp.com/docs/packages/orm/intro.html http://fuelphp.com/docs/classes/model_crud/introduction.html http://fuelphp.com/docs/packages/orm/intro.html

Both of these will you can access your database and update and create settings if needed. 这两种方法都可以访问数据库,并根据需要更新和创建设置。

Then you will want to add it to the config file to have it autoloaded. 然后,您需要将其添加到配置文件中以使其自动加载。

http://fuelphp.com/docs/general/configuration.html http://fuelphp.com/docs/general/configuration.html

You could also extend the core Config class: http://fuelphp.com/docs/classes/config.html http://fuelphp.com/docs/general/extending_core.html 您还可以扩展核心Config类: http : //fuelphp.com/docs/classes/config.html http://fuelphp.com/docs/general/extending_core.html

which will give you access to the cor config methods which include the basic getters and setters. 这将使您能够访问cor config方法,其中包括基本的getter和setter。

You can create a new Controller, eg GlobalController And extend the other controllers to the Global one. 您可以创建一个新的控制器,例如GlobalController,并将其他控制器扩展到Global控制器。

Then in GLobalController you create a before() function. 然后在GLobalController中创建一个before()函数。 In other words this function will be executed before the rest. 换句话说,此功能将在其余功能之前执行

public function before() {
    parent::before();
    $this->globalstring = "MyGlobalString"; 
}

Now, every controller who have extended the GlobalController can detect the $this->globalstring string or any other thing that you put in that function. 现在,每个扩展了GlobalController控制器都可以检测$this->globalstring字符串或您放入该函数中的任何其他东西。

The best and most transparent solution would be to extend the Config class to add DB as a supported backend. 最好和最透明的解决方案是扩展Config类,以将DB添加为受支持的后端。

http://fuelphp.com/forums/discussion/3104/dbconfig-store-your-app-specific-config-setting-in-a-db-rather-than-loads-of-config-files , altough old, might give you some pointers on how to do it. http://fuelphp.com/forums/discussion/3104/dbconfig-store-your-app-specific-config-setting-in-a-db-rather-than-loads-of-config-files (虽然年代久远)可能给您一些如何做的指示。

You should not use ORM for this, as that would require to many dependencies. 您不应为此使用ORM,因为这将需要许多依赖项。 Use regular DB calls instead. 请改用常规的数据库调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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