douping5226 2010-01-25 19:42
浏览 42
已采纳

CodeIgniter问题

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.

Thanks

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

  • 写回答

5条回答 默认 最新

  • douhao7889 2010-01-25 20:01
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题