Welcome Guest, Not a member yet? Register   Sign In
How should one organise controllers and their functions?
#1

[eluser]underskor[/eluser]
This could be a personal preference thing, but I'm just wondering what's considered better.

A)
Code:
//*******controllers/create.php********//
//Blog
function entry() {}
//Gallery
function image() {}
Code:
//*******controllers/edit.php********//
//Blog
function entry() {}
//Gallery
function image() {}

Code:
$route['blog/new/entry'] = 'create/entry';
$route['blog/edit/entry'] = 'edit/entry';
$route['gallery/new/image'] = 'create/image';
$route['gallery/edit/image'] = 'edit/image';

B)
Code:
//*******controllers/blog.php********//
function entry() {}
function edit_entry() {}

Code:
//*******controllers/gallery.php********//
function image() {}
function edit_image() {}

Can anyone point out any dis/advantages of using either scheme?

underskor
#2

[eluser]nikefido[/eluser]
Well off the cuff I think I can offer some hopefully useful insight (I won't say it's a definitive answer Tongue )

What schema you choose can really be what works best for your application. Using schemas to keep things standard helps both the developer and others who might edit the code.

What schema you choose here might be related to the size of your application.

One tenant of OOP is to keep code modular - to not let classes have too much information or do too much, as doing so increases coupling between classes.

I would therefore prefer schema B. This allows methods related to "blog" to be in one area, while "gallery" methods to be in another. This will be handy if you have a potential growing number of areas (for instance, expanding on Blog and Gallery and adding, say, CMS, music player or video gallery, etc)

If the scope of your application is smaller and unlikely to go beyond "gallery" and "blog", than schema A may help.

Schema A can get messy since it would be handling functionality for many areas if you expand upon "blog" and "gallery".

I hope this makes sense! (and others agree with me Tongue )
#3

[eluser]mvdg27[/eluser]
Setup B makes more sense to me as well. I think this setup indeed makes it easier to add functionality to a part of your application (new function for your blog) and add new controllers to your application.

What you may try to do as well, is create one model that handles all basic CRUD operations. I've been experimenting with this, and it seems to work really well. For example, I prepare the data to be updated in the database in my controller (or model for the controller) and then let a 'general_model' do the actual work. This allows me to easily add modification date and the user ID of the person that modified the record for all controllers.

Code:
function update_item($table, $item_id, $db_input) {
    
    // set modification date and user_id
    $db_input['modified'] = date('Y-m-d H:i:s');
    $db_input['modifier'] = $this->session->userdata('user_id');
        
    // update the table
    $this->db->where('id', $item_id);
    $this->db->update($table, $db_input);
            
    // check if rows were updated
    if(!$this->db->affected_rows()) return false
    else return true;

}

Hope that helps.

Michiel
#4

[eluser]jalalski[/eluser]
I use B although with different function names (I prefer simple 'edit', 'delete' etc.).

Your controllers are what gets called by CI based on the URL. So the two different schemas above give a different URL:

myserver.com/index.php/create/entry
or
myserver.com/index.php/blog/entry

I like to have:
object -> action -> entries
myserver.com/index.php/blog/edit/24
#5

[eluser]underskor[/eluser]
[quote author="nikefido" date="1231753183"]One tenant of OOP is to keep code modular - to not let classes have too much information or do too much, as doing so increases coupling between classes.[/quote]

Sold! That is a very good reason, probably the main reason why I will stick with B).

[quote author="mvdg27" date="1231772727"]What you may try to do as well, is create one model that handles all basic CRUD operations.[/quote]

I was reading about this on here earlier today, looks interesting. I'm not sure how much I'd gain from it in my current application though, as my models are pretty much only CRUD anyway. Maybe I just need to do more reading on it.

Code:
//Categories Model
var $category_id;
var $category_title;
var $category_desc;
var $category_live;

function edit_category() {
    $category_data = array(
                'category_title'    => $this->category_title,
                'category_desc'     => $this->category_desc,
                'category_live'     => $this->category_live
                );

    $this->db->where('category_id', $this->category_id);
    $this->db->update('blog_categories', $category_data);
    $this->db->limit(1);
    
    if($this->db->affected_rows() > 0) {
        return TRUE;
    } else {
        echo $this->db->last_query(); //Debug purposes
        return FALSE;
    }
}

Code:
//Controller Example
$this->category_model->category_id = $category_id;
$this->category_model->category_title = $_POST['category_title'];
$this->category_model->category_desc = $_POST['category_desc'];
$this->category_model->category_id = $_POST['category_live'];
$update = $this->category_model->edit_category();

if($update !== FALSE) {
    echo 'Updated';
} else {
    echo 'Failure';
}

Maybe someone can shed some light on CRUD and whether or not it's worth it in this situation.
#6

[eluser]mvdg27[/eluser]
Well, it's by no means necessary to use an extra model for CRUD operations. Just in my case (a CMS) I kept writing the same functionality over and over again. That should be the decision point for you: do you use the same functions in different models/ controllers?

My earlier example about storing the modification date and modifier was one of these tasks. Now it's automatically added everywhere. Now for example if I was to decide that I also want to store the IP of the person making the last modification, I wouldn't have to add this for every controller, but just in this one model.

Another function that I have in this 'general_model' is 'save_tree'. As almost all of my CMS modules are based on menustructures and they are all stored the same way, it was beneficial to use only one function for that, instead of copy-pasting it every time.

All in all, it depends if it makes sense for you to have a a 'general' model as well. In my situation it makes the code better to read and easier to maintain.

Michiel
#7

[eluser]nikefido[/eluser]
Don't forget about capitalizing on PHP OOP capabilities, especially if you are in a php5 environment




Theme © iAndrew 2016 - Forum software by © MyBB