0

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)?

2 Answers 2

1

I give you some clue.

From my experience and what i seen:

Controller is used when it serve formated data like text, json, html etc.
Or used when it receive inputs and redirect to other page eventualy.
Controller can have some privates methods used by other methods to format or check some data.

Libraries can be bridge between Controllers and Models.
They works with models or other libraries or just native PHP.
Commonly libraries works with data received by controllers and pass them to models after some check or work with them.

Libraries can be also classes,representing an entity, you have to instanciate it like $Obj = new SomeObject() (you have to include the class file).
In that case the Singleton pattern is not used.

  • So if your Controller receive the input credit_brought
  • it check if credit_brought is a number (eventualy)
  • it call the proper class method $User->add_credit()
    • That method can rise an exception if credit_brought is not a number
    • it can do some check like "is this user earn enough credit for that hour"
    • That method can call the model $this->user_model->add_credit()
      • To make the SQL Query, for example: $this->db->set('credit', 'credit +'.$credit, FALSE)->where('id', $userid)->update('users')
  • When all done, $User->add_credit() return some response (bool in that case) to the controller
  • The controller return the appropriate data to the client (view or ajax response or..)

I'm not sure if i was clear with my poor english..

In CI doc, read the Introduction and General Topics pages that you can find in the table of contents

Please correct me if you see any spelling mistakes.

Sign up to request clarification or add additional context in comments.

3 Comments

So, if I understand correctly (focusing on User for example), you're saying I should create the User class as a library, then create a $User object in MainController and use User's methods from there to return the needed data?
Yes, but create an object if you have multiple "User" like an array of User object, in other case load the class by CI Loader.
User is the logged in user, so there's only one at any given time.
1

I think it's better to separate the Game and User controller as they represent different resources. But to answer your questions by bullets:

  1. Yes UserController and GameController should extend CI_Controller to be able to respond incoming requests that comes from the front controller.
  2. They are placed in controllers folder by default.
  3. Well, the MainController's index() method creates the JS objects whose will interact with UserController and GameController methods using AJAX requests.

4 Comments

Thanks. But I'm not sure I understand #3. Let's leave the client side out of this for now please, assume there's no AJAX, just all the data generated through PHP and inserted into the template. If I create the two new controllers, then what is the best way to associate them with MainController? Should I use their methods from within the MainController in some way?
I am not sure what do you mean by 'associate'. Could you elaborate more?
Sorry, I mean, in this scenario MainController should be able to use UserController's methods, right? So, should I create a $User object within MainController by loading the User class and then use that?
OK, I get it. By CI's design, Controller's methods should only: 1) Redirect, by calling redirect() explicitly, 2) instantiate Model and Library classes. Controller's methods are not designed to be invoked from other controller except from redirect(). For example, you may want redirect() someone from Admin::index() to Main::index() if they are not logged in.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.