Coding Style

A sample structure of a KISSMVC controller function is shown below:

<?php
//This is an example of a KISSMVC controller
//It is simply a function, which will be called by an URL such as:
//http://example.com/main/showarticle/234
//TIP: Please assign default values to all parameters

function showarticle($articleid=0) {
//SECTION 1: THE LORE
//Your comments, changelog etc goes here

//SECTION 2: THE GATHERING
//Include your libraries, models, database handler, helpers here
  
require_once(APP_PATH.'models/user.php');
  require_once(
APP_PATH.'models/article.php');
  require_once(
APP_PATH.'helpers/table.php');
  
$dbh getdbh(); //getdbh() is defined in APP_PATH/index.php

//SECTION 3: THE OFFERING
//Filter, sanitize and store all inputs used by this controller
  
$articleid=min(0,(int)$articleid); //zero or positive integer
  
$uid = isset($_SESSION['authuid']) ? $_SESSION['authuid'] : 0;
  
$someconfig $GLOBALS['registry']['someconfig']; //can you trust your $GLOBALS array?

//SECTION 4: THE TOIL
//Call functions/objects that implement your business logic with the inputs from SECTION 3
//Data returned from your Model should not contain UI specifics (such as html code)
  
$loggedinuser = new User();
  
$loggedinuser->retrieve($uid);

  
$article = new Article();
  
$article->retrieve($articleid);

//SECTION 5: THE BESTOWAL (THE REVELATION?)
//Call the view templates with the data obtained from SECTION 4
//A change in UI should only affect code in this section
//Sometimes there is no output needed, only a header redirect is returned
  
if (!$loggedinuser->hasPermission('view_article')) {
    
$data['body']='<p class="error">You have no permission to access this page!</p>';
    
viewDump('layouts/mainlayout.php',$data);
    exit;
  }
  
  
$data['article']=$article;
  
//article templated is defined in views/layout/view_article.php
  
$data['body']=viewFetch('layouts/view_article.php',$data);
  
viewDump('layouts/mainlayout.php',$data);


}