Welcome Guest, Not a member yet? Register   Sign In
Extending controllers out of the 'core' directory
#11

Thanks Narf and InsiteFX for your comments.

I double checked as stated since I did express doubts on my earlier statement. Placing two or more base Controller class definitions within the single MY_Controller.php file works well.

So for those reading this conversation later on, here is a summary.

If you are trying to setup more than one BASE controller to achieve something like following class hierarchy.

CI_Controller
MyCustomFrontController extends CI_Controller
News extends MyCustomFrontController
Login extends MyCustomFrontController
About extends MyCustomFrontController
MyCustomAPIController extends CI_Controller
MembersDirectoryService extends MyCustomAPIController
ActiveUserListService extends MyCustomAPIController


All you need to do is:
- Create a MY_Controller.php file under application/core. Use MY_ or whatever prefix you configured in application/config (subclass_prefix)
- In the MY_Controller.php file declare all your base classes extending from CI_Controller or any other class within that same file.
- Create your controllers extending them from one of your many base Controllers defined in MY_Controller.php


I still wished I could have multiple base controllers, each defined in their own file baring the same name as the class. But I can live with this alternative.

Nice to find a forum that is active.

Karl
Reply
#12

(03-14-2016, 08:32 PM)kbrouill Wrote: Thanks Narf and InsiteFX for your comments.

I double checked as stated since I did express doubts on my earlier statement. Placing two or more base Controller class definitions within the single MY_Controller.php file works well.

So for those reading this conversation later on, here is a summary.

If you are trying to setup more than one BASE controller to achieve something like following class hierarchy.

CI_Controller
     MyCustomFrontController extends CI_Controller
           News extends MyCustomFrontController
           Login extends MyCustomFrontController
           About extends MyCustomFrontController
     MyCustomAPIController extends CI_Controller
           MembersDirectoryService extends MyCustomAPIController
           ActiveUserListService extends MyCustomAPIController


All you need to do is:
- Create a MY_Controller.php file under application/core. Use MY_ or whatever prefix you configured in application/config (subclass_prefix)
- In the MY_Controller.php file declare all your base classes extending from CI_Controller or any other class within that same file.
- Create your controllers extending them from one of your many base Controllers defined in MY_Controller.php


I still wished I could have multiple base controllers, each defined in their own file baring the same name as the class. But I can live with this alternative.

Nice to find a forum that is active.

Karl

very good solution.
thank you Karl
Reply
#13

You can have separate files if you add this to the bottom of ./application/config/config.php

PHP Code:
/*
| Autoloader function
|
| Add to the bottom of your ./application/config/config.php
|
| All Class files should be placed in the ./application/core folder
|
| @author Brendan Rehman
| @param $class_name
| @return void
*/
function __autoloader($class_name)
{
    
/**
     * class directories
     * 
     * add more autoloading folders here… and you’re done.
     */
     
$directories = array(
     
    APPPATH.'core/',
     );
 
  
    
// for each directory
    
foreach ($directories as $directory)
    {
        
// see if the file exsists
        
if (file_exists($directory.$class_name.'.php'))
        {
            
// only require the class once, so quit after to save effort (if you got more, then name them something else
            
require_once($directory.$class_name.'.php');

            return;
        }
    }
}

spl_autoload_register('__autoloader'); 

Just add the Autoloader.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB