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

[eluser]Naki[/eluser]
Hi all!

I'm new to CI, I've read the docs and tutorials and it looks really good ^^.

So I went to step 2 : coding for my real app :p . And then, I'm a bit lost on how to create a clean classes hierarchy (so this post is quite a mix between questions and Work In Progress, as I'm writing and trying at the same time Wink )

Here is what I'd like to have :
Code:
Controller
  |- Class1
  |   |- Subclass11
  |   |- Subclass12
  |   |- Subclass13
  |
  |- Class2
      |- Subclass21
      |- Subclass22

First, I've tried to create everything in my application/controllers folder, but then I have a "Class not found" error when I try to write
Code:
class Subclass11 extends Class1

Then I've read the doc again, and it seems that Class1 and Class2 should be libraries, is it right?

Finally, I've been able to create subclasses (in controllers) if Class1 and Class2 are created in application/libraries/MY_Controller.php. But if I have 10 classes, it will become difficult to read all classes in the same file. Is it possible to write them in separate files?

Thanks a lot for your help!
#2

[eluser]Michael Ekoka[/eluser]
You can create controllers base classes under application/libraries/MY_Controller.php, if you want to include additional files there's a family of nifty functions called... tadaaa!

include and require :lol:

were you expecting something else?

CodeIgniter just gives you the basics architecture, but you still have a great deal of flexibility when it comes to organizing your files. I see nothing wrong with:

application/libraries/MY_Controller.php
Code:
require_once (APPPATH."libraries/base_controllers/application.php");
require_once (APPPATH."libraries/base_controllers/admin.php");
require_once (APPPATH."libraries/base_controllers/form.php");
// etc...
#3

[eluser]Michael Wales[/eluser]
Here's an actual example from a personal project of mine:

application/libraries/MY_Controller.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Public_Controller extends Controller {
    function public_controller() {
        parent::Controller();
        $this->data->theme = $this->options->get('theme');
        $this->data->user = $this->auth->getUser($this->session->userdata('user_id'));
    }
}

class Admin_Controller extends Controller {
    function admin_controller() {
        parent::Controller();
        $this->data->user = $this->auth->getUser($this->session->userdata('user_id'));
        if (!$this->data->user) {
            redirect('user/login');
            return;
        }
    }
}
?>

controllers/home.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Public_Controller {
    function __construct() {
        parent::Public_Controller();
        $this->load->library('pages');
    }

    // More stuff here
?>
#4

[eluser]Naki[/eluser]
Thank you for your help, everything's clear now ^^
#5

[eluser]Michael;[/eluser]
[quote author="Michael Wales" date="1198196469"]Here's an actual example from a personal project of mine:

application/libraries/MY_Controller.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Public_Controller extends Controller {
    function public_controller() {
        parent::Controller();
        $this->data->theme = $this->options->get('theme');
        $this->data->user = $this->auth->getUser($this->session->userdata('user_id'));
    }
}

class Admin_Controller extends Controller {
    function admin_controller() {
        parent::Controller();
        $this->data->user = $this->auth->getUser($this->session->userdata('user_id'));
        if (!$this->data->user) {
            redirect('user/login');
            return;
        }
    }
}
?>

controllers/home.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Public_Controller {
    function __construct() {
        parent::Public_Controller();
        $this->load->library('pages');
    }

    // More stuff here
?>
[/quote]


Just a real quick query on the way you have this set up... I have been coding since the mid 90's but have *NEVER* touched anything OO.

Since "Public_Controller" extends "Controller" anything that is in the named function of "Public_Controller" will be run automagically any time a controller is loaded that extends "Public_Controller" ... and the same for "Admin_Controller"? Or, any other class that ends up in My_Controller.php, correct?

Thanks...
Michael




Theme © iAndrew 2016 - Forum software by © MyBB