Crud Controller
There are three main styles of forms/controllers found in ClearOS. This document provides information on the basic structure of a CRUD controller. The Form Controller documentation provides a more detailed description of every line in the controller, so if you need help with understanding how to use something like $this→form_validation→set_policy, the form controller document is a better reference.
You can find more information about the other two styles of controllers here:
Copyright and License
Please make sure you have a license notice at the top of your source code file.
Class Declaration
All controller classes must extend the ClearOS_Controller class. Also, don't forget to add the documentation block that describes your controller class!
Methods
Summary
In a typical ClearOS CRUD controller, there are 5 standard methods to think about:
ClearOS controller name | CRUD Equivalent | Description |
---|---|---|
index | read | show item or summary of items view |
add | create | show the add item view |
edit | update | show the edit item view |
delete | delete | show the delete item view (confirmation dialog box) |
destroy | delete | deletes the given item |
index
The index method is typically just a simple data gathering exercise using method calls to the underlying API. The corresponding CRUD View displays the data in a summary format. The three basic steps are:
- Load the necessary libraries
- Gather the data for the view
- Call the view
function index() { // Load dependencies //------------------- $this->load->library('network/Hosts'); $this->lang->load('dns'); // Load view data //--------------- try { $data['hosts'] = $this->hosts->get_entries(); } catch (Exception $e) { $this->page->view_exception($e); return; } // Load views //----------- $this->page->view_form('dns/summary', $data); }
add / edit
In a CRUD application, the add and edit forms are nearly identical. Typically, there are really only a few differences between the two:
- Form handler - add and edit might have slightly different form handling requirements
- Form buttons (Add/Cancel versus Update/Delete/Cancel)
- Read only key field (e.g. username is read only in the edit view)
This document provides an example using a combined add/edit form. In our example, both add and edit call the private _add_edit method as follows:
function add($ip = NULL) { $this->_add_edit($ip, 'add'); } function edit($ip = NULL) { $this->_add_edit($ip, 'edit'); } function _add_edit($ip, $form_type) { // Handling for both add and edit }
delete
The delete view shows a simple confirmation dialog box for deleting an item. Method calls to the underlying API are not necessary.
destroy
The destroy method in the controller will delete an item without confirmation from the end user. Once an item is deleted, the user is redirected back to the summary view. A call to $this→page→set_status_deleted is a way to pass information about a successful delete. The theme designer defines how to handle these messages.
function destroy($ip = NULL) { // Load libraries //--------------- $this->load->library('network/Hosts'); $this->load->library('dns/DnsMasq'); // Handle form submit //------------------- try { $this->hosts->delete_entry($ip); $this->dnsmasq->reset(); $this->page->set_status_deleted(); redirect('/dns'); } catch (Exception $e) { $this->page->view_exception($e); return; } }