Udi Mosayev

Programming, Business and my thoughts..

Developing Content Managment System With CodeIgniter – Part 2

Hi, if  you haven’t read my previous post, please read it now: Developing Content Management System With CodeIgniter Part 1.

MY_Controller and other extended core libraries

As I told you, I always write my own controllers that extends CI’s Controllers. The method is simple, I write 2-3 Controllers and looking for similarities.

class Add extends Controller {

    public function Add() {
        parent::Controller();
        log_message('debug', 'User / Add Class Initialized.');
    }

    public function index() {
        // Loading the new user form..
    }

    public function process() {
        // Getting the input and activate
    }
}

The example above is a class for creating new user. index() methods loads the view of the new user form, and the process() gets the post data and inserts it to the database.
Controllers like this I have almost in each module I create, so why not writing this MY_Controller class? look:

class MY_Controller extends Controller {
    // The name of the module, User, Catalog, Photo Gallery..
    private $moduleName;

    // Add New Form
    private $addNewView;

    public function MY_Controller() {
        parent::Controller();

    }

    public function index() {
        $content = $this->load->view($this->moduleName.'/'.$this->addNewView, '', TRUE);

        // As I told you before, I create a TPL library that handles all my template stuff.
        $this->tpl->createTemplate($content);
    }

    public function process() {
        // This is the only function I write for each controller like this,
        // this method validates the data and activates the proper model
        // to insert this data in DB.
    }
}

So my new User Add class will look like this:

class Add extends MY_Controller {
    // The name of the module, User, Catalog, Photo Gallery..
    private $moduleName;

    // Add New Form
    private $addNewView;

    public function Add() {
        parent::MY_Controller();
        // Add New Form - the view I have in "views/user/" -
        // remember the module method from the last article?
        $this->addNewView = 'add';

        // This means that the Add.php class is sitting in controllers/user folder.
        $this->moduleName = 'user';
    }

// Because I already implemented index() method in MY_Controller class,
// All I left to do is write my own process() method that is unique for each controller.

    public function process() {
        // In my view, all the inputs of my form named data[xxx] -
        // for example: data[FirstName] and data[LastName], so I grab them as an array.
        $data = $this->input->post('data');

        // Some validation and manipulation..
        $this->user_model->add($data);

        // some more stuff..
    }
}

Conclusion

This concept in great for models too, if you’re not using ORM, you can create basic save() & deleted() methods in your MY_Model class.
The only thing you should do to see if you need this: look for similar code between controllers and models, DO NOT WRITE THE SAME CODE TWICE.

I hope my next part will come faster then this one.

Related Posts


View Comments

Written by Udi Mosayev

December 19th, 2009 at 6:28 pm

View Comments to 'Developing Content Managment System With CodeIgniter – Part 2'

Subscribe to comments with RSS or TrackBack to 'Developing Content Managment System With CodeIgniter – Part 2'.

  1. Great tutorials, please keep posting. I’m learning a lot good stuff!

    Thakyou

    Carlos

    1 Jan 10 at 18:00

  2. Thanks for the comment.

    If there’s any questions or problems you’re facing in web development field please let me know so I could write better posts.
    Make sure to checkout the new post in this series!

    Udi Mosayev

    1 Jan 10 at 22:08

  3. very nice.
    I love your tuts

    Prabhjeet

    8 Mar 10 at 10:18

Leave a Reply

blog comments powered by Disqus