CodeIgniter Forums
Extending controllers out of the 'core' directory - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Extending controllers out of the 'core' directory (/showthread.php?tid=64271)

Pages: 1 2


Extending controllers out of the 'core' directory - jlamim - 02-02-2016

Today I was approached by a student of mine who is starting in CI and he asked me a question that I was right in question to answer. He wants to extend a controller, in the following situation:

Father Controller -> class extends Person CI_Controller
Controller son -> class PersonEspecial extends Person

However, he only managed to make it work by putting the controller in the parent directory 'core'. He would like to extend the controller maintaining the controller in the parent directory 'controllers'.

I found no reference in the documentation and I wonder if anyone could give an answer on the issue.

It can extend a controller even though he was within 'application / controllers'?


RE: Extending controllers out of the 'core' directory - jlp - 02-02-2016

core/MY_Controller *is* the intended way to provide controllers that can be extended.

Your MY_Controller source code file can always contain Person (extends CI_Controller), Father (extends Person), and so on.


RE: Extending controllers out of the 'core' directory - jlamim - 02-02-2016

Tks jlp!!!

(02-02-2016, 01:09 PM)jlp Wrote: core/MY_Controller *is* the intended way to provide controllers that can be extended.

Your MY_Controller source code file can always contain Person (extends CI_Controller), Father (extends Person), and so on.



RE: Extending controllers out of the 'core' directory - kbrouill - 03-13-2016

(02-02-2016, 11:54 AM)jlamim Wrote: Today I was approached by a student of mine who is starting in CI and he asked me a question that I was right in question to answer. He wants to extend a controller, in the following situation:

Father Controller -> class extends Person CI_Controller
Controller son -> class PersonEspecial extends Person

However, he only managed to make it work by putting the controller in the parent directory 'core'. He would like to extend the controller maintaining the controller in the parent directory 'controllers'.

I found no reference in the documentation and I wonder if anyone could give an answer on the issue.

It can extend a controller even though he was within 'application / controllers'?


My question had strong similarities to this one so I figured I would ADD mine in hopes of ideas.

I understand that CI_Controller is the base controller provided by CodeIgniter and that we can extend this base controller with our own version named MY_Controller (MY_ being a defined prefix of the config.

My objective is two have TWO custom base controllers:

MyCustomFrontController
This BASE controller is pretty much what I would have put in the classic MY_Controller and then used it as the BASE for all my regular controllers. This controller is specialized to handle my USER AUTHENTICATION needs.

MyCustomAPIController
This BASE controller would be used for controllers that are in fact services exposed as RESTful APIs and used as the BASE for all my services. Handling for example HTTP BASIC AUTHENTICATION commonly used in API calls.


My Controller inheritance tree would look like this:

CI_Controller
       MyCustomFrontController
            News
            Login
            About
      MyCustomAPIController
            MembersDirectoryService
            ActiveUserListService
        

Any tips on how I could achieve this cleanly?

So far, I am tempted to alter part of the CI core code to add support for multiple base controllers.

Thanks in advance for any tips, this is my first forum post  :-)


RE: Extending controllers out of the 'core' directory - InsiteFX - 03-13-2016

Base_Controller extends CI_Controller {} // Save as ./application/core/MY_Controller.php

CustomFrontController extends Base_Controller {} // save as ./application/core/CustomFrontController.php

CustomAPIController extends Base_Controller {} // save as ./application/core/CustomAPIController.php


RE: Extending controllers out of the 'core' directory - Narf - 03-13-2016

(03-13-2016, 08:24 PM)kbrouill Wrote:
(02-02-2016, 11:54 AM)jlamim Wrote: Today I was approached by a student of mine who is starting in CI and he asked me a question that I was right in question to answer. He wants to extend a controller, in the following situation:

Father Controller -> class extends Person CI_Controller
Controller son -> class PersonEspecial extends Person

However, he only managed to make it work by putting the controller in the parent directory 'core'. He would like to extend the controller maintaining the controller in the parent directory 'controllers'.

I found no reference in the documentation and I wonder if anyone could give an answer on the issue.

It can extend a controller even though he was within 'application / controllers'?


My question had strong similarities to this one so I figured I would ADD mine in hopes of ideas.

I understand that CI_Controller is the base controller provided by CodeIgniter and that we can extend this base controller with our own version named MY_Controller (MY_ being a defined prefix of the config.

My objective is two have TWO custom base controllers:

MyCustomFrontController
This BASE controller is pretty much what I would have put in the classic MY_Controller and then used it as the BASE for all my regular controllers. This controller is specialized to handle my USER AUTHENTICATION needs.

MyCustomAPIController
This BASE controller would be used for controllers that are in fact services exposed as RESTful APIs and used as the BASE for all my services. Handling for example HTTP BASIC AUTHENTICATION commonly used in API calls.


My Controller inheritance tree would look like this:

CI_Controller
       MyCustomFrontController
            News
            Login
            About
      MyCustomAPIController
            MembersDirectoryService
            ActiveUserListService
        

Any tips on how I could achieve this cleanly?

So far, I am tempted to alter part of the CI core code to add support for multiple base controllers.

Thanks in advance for any tips, this is my first forum post  :-)

2 base controllers for a total of 5 pages? That's what I call an over-engineered solution.

(03-13-2016, 10:11 PM)InsiteFX Wrote: Base_Controller extends CI_Controller {} // Save as ./application/core/MY_Controller.php

CustomFrontController extends Base_Controller {}  // save as ./application/core/CustomFrontController.php

CustomAPIController extends Base_Controller {}  // save as ./application/core/CustomAPIController.php

That won't work.

Just declare all the classes inside the MY_Controller.php file.

... which is what jlp said.


RE: Extending controllers out of the 'core' directory - InsiteFX - 03-14-2016

(03-13-2016, 11:03 PM)Narf Wrote:
(03-13-2016, 08:24 PM)kbrouill Wrote:
(02-02-2016, 11:54 AM)jlamim Wrote: Today I was approached by a student of mine who is starting in CI and he asked me a question that I was right in question to answer. He wants to extend a controller, in the following situation:

Father Controller -> class extends Person CI_Controller
Controller son -> class PersonEspecial extends Person

However, he only managed to make it work by putting the controller in the parent directory 'core'. He would like to extend the controller maintaining the controller in the parent directory 'controllers'.

I found no reference in the documentation and I wonder if anyone could give an answer on the issue.

It can extend a controller even though he was within 'application / controllers'?


My question had strong similarities to this one so I figured I would ADD mine in hopes of ideas.

I understand that CI_Controller is the base controller provided by CodeIgniter and that we can extend this base controller with our own version named MY_Controller (MY_ being a defined prefix of the config.

My objective is two have TWO custom base controllers:

MyCustomFrontController
This BASE controller is pretty much what I would have put in the classic MY_Controller and then used it as the BASE for all my regular controllers. This controller is specialized to handle my USER AUTHENTICATION needs.

MyCustomAPIController
This BASE controller would be used for controllers that are in fact services exposed as RESTful APIs and used as the BASE for all my services. Handling for example HTTP BASIC AUTHENTICATION commonly used in API calls.


My Controller inheritance tree would look like this:

CI_Controller
       MyCustomFrontController
            News
            Login
            About
      MyCustomAPIController
            MembersDirectoryService
            ActiveUserListService
        

Any tips on how I could achieve this cleanly?

So far, I am tempted to alter part of the CI core code to add support for multiple base controllers.

Thanks in advance for any tips, this is my first forum post  :-)

2 base controllers for a total of 5 pages? That's what I call an over-engineered solution.

(03-13-2016, 10:11 PM)InsiteFX Wrote: Base_Controller extends CI_Controller {} // Save as ./application/core/MY_Controller.php

CustomFrontController extends Base_Controller {}  // save as ./application/core/CustomFrontController.php

CustomAPIController extends Base_Controller {}  // save as ./application/core/CustomAPIController.php

That won't work.

Just declare all the classes inside the MY_Controller.php file.

... which is what jlp said.

It works if you use spl_autoload


RE: Extending controllers out of the 'core' directory - Narf - 03-14-2016

(03-14-2016, 03:26 AM)InsiteFX Wrote: It works if you use spl_autoload

A lot of things could work with an autoloader, but you can't assume it when giving suggestions to other people.


RE: Extending controllers out of the 'core' directory - kbrouill - 03-14-2016

Thanks for the input.

No it is not over engineered, I noted examplessssssss, not about to dump an ls on a question post :-)

Also the point is to have TWO (different) base controllers, each extending from the CI_Controller.

Result:
I had not considered declaring more than one class in My_Controller since I normally prefer to stick to a single class per file. Further more, having gone though CI's entire process, I am not sure two classes in one file would work since it doesn't only load the file, it also instantiates the class baring the same name as the file. (But I will double check).

Thanks


RE: Extending controllers out of the 'core' directory - Narf - 03-14-2016

(03-14-2016, 12:57 PM)kbrouill Wrote: Thanks for the input.

No it is not over engineered, I noted examplessssssss, not about to dump an ls on a question post   :-)

Also the point is to have TWO (different) base controllers, each extending from the CI_Controller.

Result:
I had not considered declaring more than one class in My_Controller since I normally prefer to stick to a single class per file. Further more, having gone though CI's entire process, I am not sure two classes in one file would work since it doesn't only load the file, it also instantiates the class baring the same name as the file. (But I will double check).

Thanks

The MY_Controller (capitals, not 'My' - this is important) class is never directly instantiated - that's a fact, not a random idea that we're throwing at you. Smile