Global Loader Class |
[eluser]whobutsb[/eluser]
Hello All, Currently I have been managing multiple CI projects on a server, and a lot of times I find myself rewriting the same helpers, models, and libraries, over and over. I would like to simplify my coding and create a "global" folder to house my globally used files. I"m struggling to figure out how I would setup a global libraries folder. Here is what my current development directory structure: Code: /www My plan would be to extend the CI_Loader class or create my own "GlobalLoader" library so that I could call the global library, model, and helper files from my CI projects. But I'm struggling with loading the classes so that they will work with either CI global object of $this or $CI. Has anyone tried writing something like this or are there other forum posts that would have hints on going about doing this? Thank you for the help!
[eluser]bretticus[/eluser]
Most people prefer to implement modular separation in similar scenarios. MS allows you to copy modules to and from application folders as needed. However, I understand that you just want a global folder for common functions, etc. It's actually easier than you think. Please note: I've never done this, but all you need to do is specify a path in your index.php file to a common system folder. The CI Loader function eventually checks the system folders for files to load. For example, your index.php files: Code: /* Make sure each one of your application index.php files have the same path. You can remove the system folders from each one as they are not needed. Next, add "common" folders to your system folders: Code: /path/to/common/system/helpers/common Doing this will make it slightly easier to upgrade system, but the real benefit is to distinguish "common" files from application-specific files. It should be as easy as: Code: $this->load->helper('common/foo'); and Code: $this->load->library('common/bar'); This all *should* work. Please try it and let us know.
[eluser]whobutsb[/eluser]
Thank your for the response! I'm still unclear where in the index.php I would add a path to the global folder. I'm also not looking to recreate a complete "system" folder, I would like to just create libraries, models, and helpers.
[eluser]bretticus[/eluser]
The "boostrap" file for the whole framework is the index.php file. You must have a virtual root for each website. Thus, you could say that you need an application per virtual root. Each of your applications must have the following files at minimum: Quote:|-- application You would then have just one copy of the system folder outside of each virtual root (the same one) that you refer to in index.php. The code is all 1.7.2 core so there's no need to change the system folder other than making your common folders to house your global libraries and helpers.
[eluser]whobutsb[/eluser]
I understand the index.php is the bootstrap file. Is this how you want me to setup my project directories? Code: /www I'm just not sure if i'm following you correctly with what i'm supposed to be doing to access the Global folder and the classes.
[eluser]bretticus[/eluser]
That looks right. Also, if this is a *NIX operating system, I prefer to use the whole path to avoid any future ambiguity. Code: //set $system_folder = '../Global/system'; becomes something like: Code: //set $system_folder = '/var/www/Global/system'; But if that works and that's what you prefer, sounds okay to me. However, now you need to refer to your global libraries as: Code: $this->load->library('Global/bar');
[eluser]whobutsb[/eluser]
Oh wow, this is pretty huge. This will definitely help with my maintainability of my codebase. Thank you for your help on this!!
[eluser]bretticus[/eluser]
Also... I don't recommend global models if your intent will include database activity. Firstly, I'm not sure you wouldn't have to modify Loader to look in the system folder for a models folder. But more importantly, there would most likely be issues with database connectivity, etc. Better to handle your models in each individual application IMHO. ![]()
[eluser]bretticus[/eluser]
[quote author="whobutsb" date="1281485258"]Oh wow, this is pretty huge. This will definitely help with my maintainability of my codebase. Thank you for your help on this!![/quote] Let us know if it works for you. Like I said, I've never done this (not sure why really.) This may not be the prettiest way but it is a good way to do it without having to use a custom loader for all of your applications (I'd rather update a path in index.php rather than copy a MY_Loader.php into all my apps each time.) Good luck!
[eluser]whobutsb[/eluser]
In my projects in wouldn't be that bad to reuse the same models over and over again. I have a "Business Unit" which has the Long Name, Abbreviated Name, Address, Phone Number, etc. of the business units in my company. And most of the time I'm just copying and pasting the businessunit_model.php from one project to another. I might end up just creating a library and doing all my db calls in there using this new method. So far so good. My projects are all working correctly for the most part. But I'm still weary because I know I might find an area that is bugging out. It would've been nice to just create a Global folder and have 3 folders for models, libraries, and helpers. But I can keep experimenting and see how this goes. |
Welcome Guest, Not a member yet? Register Sign In |