Welcome Guest, Not a member yet? Register   Sign In
multiple Controller extender
#1

[eluser]datactrl[/eluser]
I have a CI Controller extender called My_controller.php. All my application controllers extend My_controller and work fine. Is that possible if I need more than one CI Controller extender? Such as My1_controller.php, My2_controller.php.


Jack
#2

[eluser]Colin Williams[/eluser]
Add your other controller classes to the MY_Controller.php file.

Code:
class AppController extends Controller {
   // ....
}

class AdminController extends Controller {
   // ....
}

class BlogController extends Controller {
   // ....
}
#3

[eluser]AndrewMalachel[/eluser]
Although you can add more than one class in a file, but aren't you suppose to create one class in one file?
I though it's to keep your code clean..?

Also, Controller was supposed to be called from the URI, while Model and View were supposed to be called from
Controller, right? So, if you make more than 1 class in your controller file, who's gonna call them??

Sorry if I was wrong, but that's how I build my CI 'till now...
#4

[eluser]nikefido[/eluser]
I believe you are confusing what Colin said. Colin is talking about the controller.php found in your system/library folder. The controllers found within your sytem/application/controllers folder will then extend any of the classes being created in the MY_Controller.php file.

Hope that makes sense...
#5

[eluser]datactrl[/eluser]
Sorry I might not state the problem properly.

MY_Controller.php is stored in folder of application/libraries/. It. which extends CI's Controller, has some common functions for different pages to call with ajax. So that I don't need to copy those functions on every page's controller. It's a common controller for every controller. So evry controller extends My_contoller instead of CI's Controller.

I found I need more different MY_Contollers to use within an application. SO I tried to change My_controller.php to different name. But CI won't work with that. The MY_Controller.php can implement as a common controller feature. But there can be only one within an application. Or there is any workd around?
#6

[eluser]Colin Williams[/eluser]
Put all the base controllers you need in the MY_Controller file, as I showed. This ensures each class is introduced by getting that file included. Essentially, you are piggy-backing off of CI's conventions. Then, in your controllers, choose the appropriate class to extend: e.g.
Code:
class Site_admin extends AdminController {}

Quote:Although you can add more than one class in a file, but aren’t you suppose to create one class in one file?

If that's what you prefer, keep all base controllers in their own file, then either include() them in the MY_Controller.php file, or in the actual controllers.
#7

[eluser]datactrl[/eluser]
Thanks, Colin. Use "include" will be the best way to do it.

Jack
#8

[eluser]datactrl[/eluser]
I got an error when trying to use "include" to implement the comomon functions inside a Class. After searching a while, realized that it's not allowed to have any require or include inside a class. So it won't work in this way.

Jack
#9

[eluser]Colin Williams[/eluser]
You can include and require from a class member function, absolutely. You might be trying it within the class but outside a function, which isn't allowed.

Code:
class Someclass {

   include APPPATH . 'path/to/file.php';
   // Not allowed

   function some_method() {

   }

}

What I suggested has nothing to do with the include happening within a function or method. In fact, if you did it that way, you would limit the scope of the include, which is not at all what you want. Your MY_Controller.php file would look something like:

Code:
include '/var/www/ci/application/controllers/base/admincontroller.php';
include '/var/www/ci/application/controllers/base/appcontroller.php';
// etc.




Theme © iAndrew 2016 - Forum software by © MyBB