Welcome Guest, Not a member yet? Register   Sign In
Which is the most productive way to use CodeIgniter?
#1

[eluser]aslamdoctor[/eluser]
Hi Guys, here something that is been eating my mind from very long time and I decided to finally post it here and get some reviews.

Let me explain this by one example, this is the way I use CI:

Let's say we are building a whole User module, which will contain below features
1. Register
2. Login/Logout
3. Fortgot Password
4. Reset Password
5. Change Password
6. Update Profile

So for this what I do is
Step-1. I create a model named user_mode.php and write all create/update/delete/get functions in it. So manly the functions that does database related activities.
Step-2. I create a controller named user.php and write all the methods like register(), login(), logout()...and all other mentioned in above 6 points.
Step-3. And finally I create view files for all these controller methods like register_view.php, login_view.php, forgot_password_view.php

So this is the normal workflow I follow. But later on one day I was discussing to one of my friend who is also a CI Developer, and I told him this is how I work on CI. And he started laughing at me saying, you are doing it completely wrong.. So I asked him to show me how he uses CI and I found the only difference is, in Step-2, he creates a separate Controller file for every method like register.php, login.php, forgot_password.php...

And he said it is very productive flow. I am not sure this is the right way but it is a good idea to find it out through you guys, how you use it. And what should be the most productive workflow to use CI, so that like this example, if I want to use the same user module on another project, I would be able to use it very quickly. If there is some other better workflow then this, please do post in your reply.

Thanks in advance for your valuable inputs.
#2

[eluser]jairoh_[/eluser]
your friend was laughing? he was not helping. It just depends on some situations, because there are some simple things that are not needed to be more sophisticated. Either of your ideas were not bad at all.
#3

[eluser]aslamdoctor[/eluser]
Thanks jairoh_ , I was thinking in a same way as I followed the way they explained in CI User guide. Let's see what other people says in their opinion Smile
#4

[eluser]greedyman[/eluser]
[quote author="aslamdoctor" date="1375500857"]Thanks jairoh_ , I was thinking in a same way as I followed the way they explained in CI User guide. Let's see what other people says in their opinion Smile[/quote]

You shouldn't create a controller and model do the same actions. With actions relative together, I put it into a model. Example:
Code:
<?php

class product_model extends CI_Model {

function __construct() {
  
}

function get_new_products() {

}

function get_list_banners() {
  
}

function get_featured_products() {
  
}
// and so on
}

/* End of file product_model.php */
/* Location: ./Application/modules/front/model/product_model.php */

And in controller I call them:
Code:
<?php if ( !defined('BASEPATH') ) exit('No direct script access allowed');

class Product extends MX_Controller {

public function __construct() {
  
}

public function details($category, $categoryId, $product, $productId) {
  $this->load->model('product_model');
  $this->load->model('group_model');
  
  $productId = array_pop(explode('.', $this->uri->segment(2)));
  
  $data['category'] = $this->group_model->get_group_name($categoryId);
  $data['categoryUrl'] = $category.'-'.$categoryId;
  $data['allGroupWithQuantity'] = $this->group_model->get_group_with_quantity();
  
  $data['product'] = $this->product_model->get_product($productId);
  $data['special_products'] = $this->product_model->get_special_products();
  $data['list_banners'] = $this->product_model->get_list_banners();
  
  $data['template'] = 'product';
  $data['title'] = $data['product']->tenSP;
  
  $this->load->view('master', $data);
  
}

}

/* End of file product.php */
/* Location: ./application/modules/front/controller/product.php */
For small projects, you don't need do follow this workflow. Template of website is created the first, so you should create controller and then create views. Access controller and try to display views, if there is nothing happen you create model. Get datas from database, replace code in view.




Theme © iAndrew 2016 - Forum software by © MyBB