Welcome Guest, Not a member yet? Register   Sign In
is it possible to extend an application controller?
#1

[eluser]nealumney[/eluser]
for instance?

class Someclass extends Controller
{
parent::Controller;
/*
do some things lots of if not all controllers need
*/
}
class Extendedclass extends Someclass
{
parent::Someclass;
}
function index()
{
// specific stuff for this controller;
}

?

Neal
#2

[eluser]Dam1an[/eluser]
First of all, use [ code ] blocks!
Secondly, why would you want to do this? If you have lots of functionality you need in each controller, you can extends CIs controller with MY_Controller (that goes in your application libraries)

If you have some stuff specific to just a few controllers, a library would be the way to go
#3

[eluser]Thorpe Obazee[/eluser]
Code:
require_once(BASEPATH.'libraries/Controller.php');

class Nice extends Controller {

Code:
require_once('nice.php');

class Welcome extends Nice {

You could do it this way. There 'might' be other options though but this is a quicky.
#4

[eluser]nealumney[/eluser]
Thanks for this,

I'll try and implement the MY_Controllers thing.

I am new to CI so I still learning all that is possible.
#5

[eluser]jedd[/eluser]
The standard way of extending core controllers is pretty effective, and it's unlikely that you'd want to have more than one additional level.

Note that you can change the MY_ prefix to whatever you prefer. Look at your application/config/config.php:
Code:
$config['subclass_prefix'] = 'MY_';

Of course it's probably not good to change this if you're still learning CI, as everyone else around here will be talking about MY_Controller.
#6

[eluser]louis w[/eluser]
Of course you can. This is an overview of how I have my CMS structured. Multiple levels of extending to keep everything organized.


Code:
// FILE:  controllers/application.php
class Application extends Controller {

    // Contains global application methods. Never called directly

}


// FILE: controllers/admin/admin.php
require_once(APPPATH . 'controllers/application.php');
class Admin extends Application {
    
    // Contains admin only admin global methods.

}


// FILE: controllers/admin/pages.php
require_once(APPPATH . 'controllers/admin/admin.php');
class Pages extends Admin {
    
    // Page admin controller

}


// FILE: controllers/site.php
require_once(APPPATH . 'controllers/application.php');
class Site extends Application {
    
    // Website controller    

}

This could also be cleaned up further by setting php up to autoload the classes. I prefer to include it explicitly.

I have multiple controllers which extend Admin, which all inherit both application and admin methods. Makes adding additional sections very easy.




Theme © iAndrew 2016 - Forum software by © MyBB