sveti petar
sveti petar

Reputation: 3797

What is the correct way to incorporate classes/controllers/libraries in CodeIgniter?

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

Upvotes: 0

Views: 99

Answers (2)

Ismail Faruqi
Ismail Faruqi

Reputation: 482

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.

Upvotes: 1

Aurel
Aurel

Reputation: 3740

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.

Upvotes: 1

Related Questions