Developing Content Managment System With CodeIgniter – Part 2

This post was originally published in 2009
The tips and techniques explained may be outdated.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

Tags: CMS, CMS Development, CodeIgniter, PHP, Web Development

  • Carlos

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

    Thakyou

  • Carlos

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

    Thakyou

  • http://udiudi.com/ Udi Mosayev

    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!

  • http://udiudi.com Udi Mosayev

    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!

  • http://www.webeventures.com/ Prabhjeet

    very nice.
    I love your tuts

  • http://www.webeventures.com Prabhjeet

    very nice.
    I love your tuts

WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera