简体   繁体   English

如何在Codeigniter中更改配置文件路径?

[英]How can I change config file path in Codeigniter?

I use Codeigniter framework , and you know when I try to load a config file then use it 我使用Codeigniter框架,你知道当我尝试加载配置文件然后使用它

I do something like that : 我这样做:

$this->load->config('myconfig', TRUE); 

myconfig.php file is located inside application folder ( application/config/myconfig.php) myconfig.php文件位于应用程序文件夹(application / config / myconfig.php)中

and use it like this : 并像这样使用它:

 $this->config->item('get_item', 'myconfig')

My question is : how can I change the location of myconfig file and use it properly ? 我的问题是:如何更改myconfig文件的位置并正确使用它?

I want to put the config file(s) in out folder like this : 我想将配置文件放在out文件夹中,如下所示:

  • mysite -> system(folder) mysite - >系统(文件夹)
  • mysite -> user_guide(folder) mysite - > user_guide(文件夹)
  • mysite -> myConfigFiles(folder) mysite - > myConfigFiles(文件夹)
  • mysite -> myConfigFiles(folder) / myconfig.php mysite - > myConfigFiles(文件夹)/ myconfig.php

I need to do something like this : 我需要做这样的事情:

$this->load->config(base_url.'myConfigFiles/myconfig', TRUE); 

any help ? 任何帮助?

Yes - it is possible to do this. 是的 - 有可能这样做。 The loader will accept ../../relative/paths . 加载程序将接受../../relative/paths You can use a path relative from the default config directory (an absolute path will not work). 您可以使用默认配置目录中的相对路径(绝对路径不起作用)。

So let's say you have this structure (had a hard time following your description): 所以,假设你有这种结构(很难按照你的描述):

  • mysite 我的网站
    • application 应用
      • config <-- default directory config < - 默认目录
    • system 系统
    • myConfigFiles myConfigFiles
      • myconfig.php myconfig.php

You can just do this: 你可以这样做:

$this->load->config('../../myConfigFiles/myconfig', TRUE);

This works for pretty much everything - views, libraries, models, etc. 这适用于几乎所有东西 - 视图,库,模型等。


Note that with the introduction of the ENVIRONMENT constant in version 2.0.1, you can automatically check for config files within the config directory in another directory that matches the name of the current environment. 请注意,通过在2.0.1版中引入ENVIRONMENT常量 ,您可以在另一个与当前环境名称匹配的目录的config目录中自动检查配置文件。 This is really intended to be a convenience method for loading different files depending on if you are in production or development. 这实际上是一种加载不同文件的便捷方法,具体取决于您是在生产还是开发中。 I'm not 100% sure what your goals are, but this additional knowledge may also help you achieve them, or it may be totally irrelevant. 我不是100%确定你的目标是什么,但这些额外的知识也可以帮助你实现它们,或者它可能完全无关紧要。

Really not sure WHY you would want to do this (and I wouldn't recommend it), but since all config files are is regular PHP files you can put a config file in the standard location that loads your extra config files. 真的不确定为什么要这样做(我不推荐它),但由于所有配置文件都是常规的PHP文件,你可以将配置文件放在加载额外配置文件的标准位置。 As an example: 举个例子:

mysite -> application -> config -> myconfigloader.php

then in myconfigloader.php put this: 然后在myconfigloader.php

<?php

require_once(APPPATH.'../myConfigFiles/myconfig.php');

So once you do 一旦你这样做了

$this->load->config('myconfigloader', TRUE); 

It will load everything in your myconfig.php file. 它将加载myconfig.php文件中的所有内容。 Let me know if that works for you. 如果这对您有用,请告诉我。

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

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