Subscribe Now: freedictionary

Add to The Free Dictionary

Tuesday, 13 March 2012

Controller - add_c.inc

This file just contains the head() and foot() functions that contain mostly plain HTML. I tend to drop out of PHP mode if I have big blocks of HTML with minimal variable substitutions. You could also use heredoc blocks here, as we saw in add.html.

Our Controller is in add_c.inc:
<?phpinclude './model/db.inc';
include 
'./model/items.inc';$db = new items();
if(
$_SERVER['REQUEST_METHOD']=='POST') {
  
header("Content-type: application/json");
  
// Load an item entry from backend and send JSON request to populate form
  
if(isset($_POST['load_item'])) {
    
$entry $db->load($_POST['load_item']);
    
$entry[0]['submit'] = 'Modify Item';
    if(
$entry) echo json_encode(array('formName'=>$_POST['formName'],
                                      
'load_item'=>$entry));
    exit;
  }
  
// Validate form fields
  
foreach($_POST as $k=>$v) {
    if(
substr($k,0,5)=="desc_") {
      if(isset(
$_POST[substr($k,5)]) && $_POST[substr($k,5)]==$v) {
        echo 
json_encode(array('validate_error'=>'f_'.substr($k,5)));
        exit;
      }
    }
  }
  
// Save changes and display status message
  
$status "Failure";
  if(
$_POST['f_submit']=='Modify Item') {
     
$ret $db->modify($_POST);
     if(
$ret$status "Modified";
  } else {
     
$ret $db->insert($_POST);
     if(
$ret$status "Added";
  }
  echo 
json_encode(array('status'=>$status,
                         
'elem'=>'tItems',
                         
'reset'=>$ret,
                         
'formName'=>$_POST['formName']));
  exit;
}
// Initialize view dataif(!isset($categories)) load_list('categories');
if(!isset(
$item)) $item = array('cat'=>'');$items $db->load();?>








Our controller is going to manipulate the model, so it first includes the model files. The controller then determines whether the request is a POST request, which means a backend request to deal with. (You could do further checks to allow an empty POST to work like a GET, but I am trying to keep the example simple.) The controller also sets the Content-Type to application/json before sending back JSON data. Although this mime-type is not yet official so you might want to use application/x-json instead. As far as the browser is concerned, it doesn't care either way.

The controller then performs the appropriate action in the model according to the specified command. A load_item, for example, ends up calling the load() method in the data model for the items table and sends back a JSON-encoded response to the browser.
The important piece here is that the controller is specific to a particular view. In some cases you may have a controller that can handle multiple very similar views. Often the controller for a view is only a couple of lines and can easily be placed directly at the top of the view file itself.


 

No comments:

Post a Comment

comment.........

Engineering -Thinks of Words

Subscribe Now: google

Add to Google Reader or Homepage