Skip to content

Instantly share code, notes, and snippets.

@okaprinarjaya
Created July 8, 2012 14:36
Show Gist options
  • Save okaprinarjaya/3071223 to your computer and use it in GitHub Desktop.
Save okaprinarjaya/3071223 to your computer and use it in GitHub Desktop.
Core Class - Controller
<?php
class Controller {
public function renderTemplate($templateName, $data = null, $contentType = 'html') {
try {
$viewObject = new View(get_called_class(), $contentType);
$viewObject->render($templateName, $data);
} catch(Exception $e) {
echo $e->getMessage();
}
}
public function setFlashMessage($message,$type) {
try {
$session = new SessionHandler();
$session->start();
$session->set('message', array('message' => $message, 'type' => $type));
$session->save();
} catch (Exception $e) {
echo $e->getMessage();
}
}
public function redirect($location = null) {
$url = '';
/*
* Pattern for String passed
* String => 'controller/action'
*/
if (is_string($location)) {
$locationPieces = explode('/',$location);
$url = $locationPieces[0].'.php?act='.$locationPieces[1];
}
/*
* Pattern for Array passed
* Array => Array (
* 'controller' => 'controller name'
* 'action' => 'action name'
* )
*/
if (is_array($location)) {
$url = $location['controller'].'.php?act='.$location['action'];
}
header('Location:'.$url);
}
public function getAction() {
$action = 'index';
if (isset($_GET['act'])) {
$action = trim($_GET['act']);
}
return $action;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment