Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Last active April 1, 2016 00:11
Show Gist options
  • Save mdmunir/729de7ce0c8963036bf36a6a715ae71d to your computer and use it in GitHub Desktop.
Save mdmunir/729de7ce0c8963036bf36a6a715ae71d to your computer and use it in GitHub Desktop.
<?php
define('APP_PATH', __DIR__);
define('SYS_GLOBAL_VAR', '_d326_fw');
session_start();
function set($name, $value)
{
$GLOBALS[SYS_GLOBAL_VAR][$name] = $value;
}
function get($name, $default = null)
{
return isset($GLOBALS[SYS_GLOBAL_VAR][$name]) ? $GLOBALS[SYS_GLOBAL_VAR][$name] : $default;
}
function createUrl($route, $params = [])
{
$url = get('baseUrl') . '/' . rtrim($route, '/');
if (!empty($params) && ($query = http_build_query($params)) !== '') {
$url .= '?' . $query;
}
return $url;
}
function redirectTo($route, $params = [])
{
header('Location: ' . createUrl($route, $params));
exit();
}
function render($view, $params = [])
{
$content = renderPartial($view, $params);
return renderFile(APP_PATH . '/views/layout.php', ['content' => $content]);
}
function renderPartial($view, $params = [])
{
if (strncmp($view, '/', 1) !== 0) { // kalau diawali '/' berarti absolut path
$view = APP_PATH . '/views/' . $view;
}
if (!is_file($view) && is_file($view . '.php')) {
$view .= '.php';
}
return renderFile($view, $params);
}
function renderFile($_file_, $_params_ = [])
{
ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE);
require($_file_);
return ob_get_clean();
}
function run()
{
set('baseUrl', $_SERVER['SCRIPT_NAME']);
$route = rtrim($_SERVER['PATH_INFO'], '/');
if (empty($route)) {
$route = 'home'; // ubah default route di sini
}
set('route', $route);
echo renderFile(APP_PATH . "/controllers/{$route}.php");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment