-
jlamim IT Specialist & Coach

-
Posts: 5
Threads: 2
Joined: Mar 2015
Reputation:
0
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'?
-
jlamim IT Specialist & Coach

-
Posts: 5
Threads: 2
Joined: Mar 2015
Reputation:
0
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.
-
kbrouill Newbie

-
Posts: 5
Threads: 1
Joined: Mar 2016
Reputation:
0
03-13-2016, 08:24 PM
(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 :-)
-
InsiteFX Super Moderator
     
-
Posts: 6,763
Threads: 346
Joined: Oct 2014
Reputation:
247
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
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
Narf Me
      
-
Posts: 1,589
Threads: 1
Joined: Oct 2014
Reputation:
121
03-13-2016, 11:03 PM
(This post was last modified: 03-13-2016, 11:04 PM by Narf.)
(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.
-
kbrouill Newbie

-
Posts: 5
Threads: 1
Joined: Mar 2016
Reputation:
0
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
-
Narf Me
      
-
Posts: 1,589
Threads: 1
Joined: Oct 2014
Reputation:
121
(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.
|