3

I use Codeigniter framework , and you know when I try to load a config file then use it

I do something like that :

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

myconfig.php file is located inside application folder ( 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 ?

I want to put the config file(s) in out folder like this :

  • mysite -> system(folder)
  • mysite -> user_guide(folder)
  • mysite -> myConfigFiles(folder)
  • mysite -> myConfigFiles(folder) / myconfig.php

I need to do something like this :

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

any help ?

1
  • I don't believe you can, why would you want to? There might be a better solution to your problem Commented Aug 20, 2011 at 6:50

2 Answers 2

6

Yes - it is possible to do this. The loader will accept ../../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
    • system
    • myConfigFiles
      • 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. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Wesley Murch , thank you very much , yes it's work fine now , sorry for the description on my question , I'm not English native =) .. thanks again for your last note , It helps me so .. have a nice day =)
Be aware that with the second parameter set to TRUE, variables declared in your config file will be stored in an array index corresponding to the path you provide in the first parameter. You will need to use $this->config->item('../../myConfigFiles/myconfig');
2

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. As an example:

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

then in myconfigloader.php put this:

<?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. Let me know if that works for you.

4 Comments

Thank you Femi , your way works fine but it will not help me , I need to load config file depends on a var that come from the Url , like this : $this->load->config('myconfigfiles/'$varFromUrl, TRUE); .. thank you for your help ..
Sure thing. I will say that whatever it is you are trying to do you should really really really reconsider letting a request parameter directly dictate your choice of configuration file: there are definitely better choices for almost any problem that could be solved with that technique.
You are right , I just want it easy for the end user to customize the script , so I want to put all config files on out folder with out digging inside application and config files , coz I need to change config , language and also view folder for every Model , so I want to grab all these stuff in one close folder and let the user create and change config and folder easily .. that is the point , I know maybe there are better ways to do that , but I cannot think more than this way /: thanks for your time ;)
Nice idea! Thumbs up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.