I'm a Codeigniter rookie, though I have about 2 years of experience with PHP. I've been through the Codeigniter documentation, and I've created some simple examples, but now I'm moving on to an actual project and I want to make sure I get off on the right foot.
Here is the issue: I'm building a one-page game application, and on that page there will be lots of dynamic/DB data. I've created a controller and view for the page, so far it's all very simple:
//controller (application/controllers/main.php) class Main extends CI_Controller { public function index() { $this->load->model('Model'); //load DB abstraction model $this->load->helper('url'); //load URL helper (base_url() function etc) $data = array("somestuff","otherstuff");//some general data for the page $this->load->view('header'); $this->load->view('mainview', $data); $this->load->view('footer'); } } //view (application/views/mainview.php) <div id="container"> <div id="tab1"> some data related to logged in user </div> <div id="tab2"> some data related to game </div> </div> Now, you see, we can split the data that should be displayed on the page into two groups - user data and application data. The natural OOP approach would be to:
1) create one User class and use a function within that to fetch and display user data (loading the Model as well since we're using MVC)
2) create one or more game-related classes and use those to calculate and display current game data
For simplicity, let's assume I just need one Game class.
What I'm having trouble with is wrapping my mind about the correct CodeIgniter approach to this. The user guide mentions user-created libraries, user-created controllers and user-created classes. The libraries are a bit of a confusing concept to me since I don't understand how they make a difference here.
Since I already have a controller for the page (I think that's the natural approach too but please correct me if I'm wrong), how do I integrate the User/Game classes? I have a few specific questions that should help me get this straight in my head.
1) Should User/Game be new controllers that extend CI_Controller?
2) Should the classes be placed in the "libraries" directory or directly in the controller?
3) What relationship should they have with the Main controller (assuming I was correct in creating it in the first place)?