Welcome Guest, Not a member yet? Register   Sign In
Codeiginiter has a procedural loader, how can I override it
#1

[eluser]Montuelle[/eluser]
Hi,

We're building a multi-site application with codeigniter 1.7.2. Basically we had 3 application
- Front office corporate website
- Front office ebusiness website
- Back office for ebusiness & CMS administration

We've organized codes with inspiration from the wiki arcticle

We need a lot of shared code & files, including common configuration files, base classes (including CI core class extension Controller, Model, Exceptions, Log, Profiler, Uri, Upload, etc.), Templating system, common helpers and hooks.

Today all of this is working, and I'm doing a bit of cleanup in code mess we've made during the developement (yeah, we modified the framework's code due to short deadlines).

I want to keep CI framework untouched as much as I can, and the only file hanging me is the system/codeigniter/Common.php which contains procedural functions (seems to be helpers show_error and show_404 are located here)

The function & load_class had to be modified to get our controller's parent including instanciated. It is defined as first CI_Controller extension MY_Controller (done like in manual), and it's located in the application's common code directory.

Because of this controller instanciation issue, I cannot rip this hack off, and as it is procedural I cannont override it properly.

I tried to use a hook to do some includes prior to controller execution, it has no effects on instanciation.

Is there any way I can extends this internal loader without actually modifying framework's code ? It seems to be a little code duplicate with CI_Loader class, which has a very similar method.
Where is the point between the two (& load_class procedural also has a class registry) ?

I precise we must work with PHP 4.3, so autoloader is not available...

Anyone who ran into similar troubles and have some advices is welcome too.

Regards,
Benoit
#2

[eluser]WanWizard[/eluser]
Why do you need to hack load_class() to get MY_Controller to work? That works out of the box, no hacks needed.

The only change we've made to make p.e. multiple controller loading possible was to ./system/codeigniter/base4.php:
Code:
class CI_Base extends MY_Loader {
    
        function CI_Base()
        {
            // This allows syntax like $this->load->foo() to work
            parent::MY_Loader();
            $this->load =& $this;
    
            // This allows resources used within controller constructors to work
            global $OBJ;
            $OBJ = $this->load; // Do NOT use a reference.
        }
    }
}
This allows a PHP4 setup to load and use our loader class extension (PHP5 works without modifications). All other functionality was added to that extension.
Our test environment is a CentOS 4 server with PHP 4.3.9.
#3

[eluser]Montuelle[/eluser]
Thanks for your answer, this is good to know !

[quote author="WanWizard" date="1274798219"]Why do you need to hack load_class() to get MY_Controller to work? That works out of the box, no hacks needed.[/quote]

Yes it worked and I used it a lot on other projects, but the developer in charge here had to do it (and I did'nt found any better solution) because our MY_Controller class is not located in application directory (APPPATH), but in our cross-application directory (internally named CMNPATH)

Perhaps I need more refactoring to leave each website with an empty MY_Controller class used only for abstraction purpose, it contains only debugger related code and fallbacks for authentification-aware page controllers.
#4

[eluser]WanWizard[/eluser]
I had something similar in that past on another project, and I ended up using 'stub loaders'.

In the libraries directories in the APPPATH's of your application, create a MY_Controller.php, and in there, add
Code:
require_once CMNPATH . 'libraries/MY_Controller.php';
to make all your 'sub-apps' work from a common code base without having to hack the core.
#5

[eluser]Montuelle[/eluser]
I'll go in this way, thank you WanWizard
#6

[eluser]Phil Sturgeon[/eluser]
Luckily in CI 2.0 you can remove this hackery and use the Application "Package" feature.

Also you could just use an autoload like the one I used in CodeIgniter Base Classes: Keeping it DRY
#7

[eluser]Montuelle[/eluser]
I read your post when I was researching on multiple application setup with codeigniter !

It's a great solution and very well described but unfortunately we have no chance to ever get PHP5 on this project Sad

A bit off-topic, but do you know if CI 2.0 will use PHP5 features like autoload ? or will it be kept php4 compatible
#8

[eluser]WanWizard[/eluser]
[quote author="Phil Sturgeon" date="1274809755"]Luckily in CI 2.0 you can remove this hackery and use the Application "Package" feature.[/quote]
I clearly have to find the time to get up to speed with 2.0. Wink

[quote author="Phil Sturgeon" date="1274809755"]Also you could just use an autoload like the one I used in CodeIgniter Base Classes: Keeping it DRY[/quote]
That doesn't help you extend the Loader class in PHP4 though...




Theme © iAndrew 2016 - Forum software by © MyBB