Welcome Guest, Not a member yet? Register   Sign In
Custom controller for testing controllers only
#1

[eluser]Mackstar[/eluser]
I have a subfolder in my controllers directory called unit testing,

in here I have controllers for that purpose.. I realized 3 methods are repeater in each of them which is not DRY enough for me. I wanted to make a custom controller which could be inherited on just these controllers and not the other controllers in my app...

So the MY_Controller concept I couldn't get working as it would apply to all controllers.

I tried loading a test_controller class using hooks but pre_system is loaded before the controller class recognized and pre_controller already has the controller file requested even though the object hasn't been constructed so gives me errors...

Is there another way to do this?? Without altering the CI system files...

Many thanks

Richard
#2

[eluser]pistolPete[/eluser]
[quote author="Mackstar" date="1238047706"]So the MY_Controller concept I couldn’t get working as it would apply to all controllers.[/quote]

No, you can easily control the inheritance.
Have a look at this example:

Code:
<?php

class Test_Controller extends Controller {
    
    function Test_Controller()
    {
        parent::Controller();
    }
    
    function something_special()
    {
        /*
         * Do something special
         */
    }
}

/* End of file MY_Controller.php */
/* Location: ./system/application/libraries/MY_Controller.php */
Code:
<?php

class Some_controller extends Test_Controller {
    
    function Some_controller()
    {
        parent::Test_Controller();
    }
}

/* End of file some_controller.php */
/* Location: ./system/application/controllers/some_controller.php */

Code:
<?php

class Another_controller extends Controller {
    
    function Another_controller()
    {
        parent::Controller();
    }
}

/* End of file another_controller.php */
/* Location: ./system/application/controllers/another_controller.php */
#3

[eluser]Mackstar[/eluser]
Thanks for the reply, yes that is great, I actually started trying this at the beginning, but it wouldn't autofind the class file and I got the following error,

Fatal error: Class 'TestController' not found in /Users/Mackstar/Sites/ccs/system/application/controllers/unit_tests/testmymodel.php on line 3

I had this file in the application/libraries directory... How can I get this to autoload??

I guess from looking at your example that you recommend placing this class in the MY_Controller.php file??

Thanks again
#4

[eluser]pistolPete[/eluser]
[quote author="Mackstar" date="1238049331"]I guess from looking at your example that you recommend placing this class in the MY_Controller.php file??[/quote]

Yes, if you name the file MY_Controller.php and place it into ./system/application/libraries/ it will be autoloaded without any further ado.
#5

[eluser]Mackstar[/eluser]
Thats great.. Have it working now...

Very quick and friendly forum, I am impressed. Might just make me stick with CI long term....




Theme © iAndrew 2016 - Forum software by © MyBB