In a CodeIgniter application that performs CRUD operations (Create, Read, Update, Delete) you need to distinguish when a controller action needs to show (read and present) or process data (typically save and delete).
The same action must respond in both cases to two different HTTP methods : GET (retrieve and show) and POST (save or delete).
Here there is the typical approach I use. (I took some inspiration from Ruby on Rails).
Typically you have at least three actions in your CRUD controller :
create: to create new recordsedit: to edit existing recordsdelete: to remove existing records
If an action is called through an HTTP GET request it will show the form or the data, if it is called through an HTTP POST request it will perform the associated operation and then it will present the form or the data.
In the example code below when the create action is called through a POST request it will call the _insert method to save the data, the edit action will call the _update method and the delete action the _remove method.
Additional checks could be added like the presence of the submit variable in the POST request : $this->post('submit');
Define a parent controller class from which you’ll inherit any subsequent controller.
This is a very convenient method to have all the necessary functions in every controller you’ll create for your application.
//file : controllers/application.php class Application extends Controller { function Application() { parent::Controller(); }
method detects which HTTP method is used to invoke the controller.
function method() { $method = 'undefined'; if(isset($_SERVER['REQUEST_METHOD'])) { $method = strToLower($_SERVER["REQUEST_METHOD"]); } return $method; }
methods that returns if the HTTP request is a GET or a POST
function method_is_get() { return ($this->method()=='get'); } function method_is_post() { return ($this->method()=='post'); } };
Then, for every controller which must implement CRUD operations you can inherit from the Application class :
//file : controllers/mycrud.php include dirname(__FILE__)."/application.php"; class Mycrud extends Application { function Mycrud() { parent::Application(); }
create action to add new records. If called via a POST request, _insert method is invoked.
The _insert method performs also the necessary data validation.
function create() { if($this->method_is_post()) { $this->_insert(); } else { $data = array(); } // show the form } function _insert() { if ($this->form_validation->run()) { // insert the record } }
edit action to edit existing records. If called via a POST request, _update method is invoked.
The _update method performs also the necessary data validation.
function edit($id) { if($this->method_is_post()) { $this->_update($id); } else { // show the form } } function _update($id) { if ($this->form_validation->run()) { // update the record } }
delete action to delete existing records. If called via a POST request, _remove method is invoked.
The _remove method performs also the necessary data validation.
function delete($id) { if($this->method_is_post()) { $this->_delete($id); } else { } } function _remove($id) { // delete the record } }
Suggested books



{ 2 comments… read them below or add one }
Hi Roberto , is CRUD near to scaffolding feature and Active Record Pattern?
Hi,
I am new to Codeigniter and looking for a good CRUD Controller that I can build off of in my project. I like the concepts you touch on here, but I would really love to get a copy of a complete controller. Would that be possible?
Thank you in advance for your help.
David
{ 1 trackback }