phpcodeigniter

Several questions about trying CodeIgniter as a framework


I'm thinking about using CodeIgniter for a new project. I was reading the user-guide for CI and I noticed a few things. In all their examples, they seem to put all the logic in the Controller and just use the model to get and set data. I like to put all my logic in my Model.

Are all of their functions universal to all 3 parts (model, view, and controller) or will there be problems if trying to do logic in the model as opposed to the controller.

Also, are all variables accessible to all 3 parts (model, view, and controller). If I wanted to know if a user was logged in within the view, would I have to pass that information to the view from the controller or is it already accessible within the view?

Also, I noticed that session data is stored within cookies, even though they are encrypted. Is the encryption safe enough to use, beause im more used to using sessions. Also, how long are these cookies stored by default? I was a little confused about that part, if anybody can clear that up.

If you have any other tips to help my learning this new framework, I would appreciate it.

EDIT: I like to use Fat Models and skinny controllers, so that I can use the same functions in more than one place.

Just read about Kohana, I think I'll look more into that


Solution

  • they seem to put all the logic in the Controller and just use the model to get and set data.

    CodeIgniter expects very little logic in its models, and instead gives you a very dumb SQL wrapper for returning simple arrays of POD types to represent your data. It even puts a lot of validation code into the controllers, which (in my opinion) is incorrect and repetitive. I've rolled my own solution for Rails-style in-model validation and dynamic find method, allowing things like

    // inside model: 
    // username must be 8 to 25 chars long
    $this->validates_length_of('username', 8, 25);
    
    // dynamically handled via __call()
    $this->User->find_first_by_username('john'); // Return object or null
    $this->User->find(); // select *
    $this->User->find_by_group('admin'); // return 0 or more records
    

    but AFAIK there isn't any built-in way of doing similar things with CodeIgniter.

    Also, are all variables accessible to all 3 parts

    No; you have to manually pass your variables from your controller to your view, and there is no sharing of variables with models/controllers or models/views.

    I believe the method suggested by CodeIgniter:

    <?php
    
    function users() {
      $data['users'] = $this->User->find(); 
    
      // must use $data['users'] for controller logic; verbose and annoying
    
      $this->load->view('users/index', $data); // $users defined for view
    }
    
    ?>
    

    can be improved by using PHP's compact keyword:

    <?php
    
    function users() {
      $users  = $this->User->find(); 
    
      // now we can use $users more easily
    
      $this->load->view('users/index', compact('users'));
    }
    
    ?>
    

    I noticed that session data is stored within cookies

    CodeIgniter can store session data in a database; see $config['sess_use_database'] in config/config.php. There are other config settings in there that pertain to the lifetime of the session cookie.

    I'm inclined to say that the only thing CodeIgniter does well is their documentation, read more about session configuration and their implemntation of active record (really a language-independant SQL wrapper which has nothing to do with the Active Record pattern)