Skip to content

Instantly share code, notes, and snippets.

@Phunky
Created November 21, 2014 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phunky/047a6a19123cf157f6d8 to your computer and use it in GitHub Desktop.
Save Phunky/047a6a19123cf157f6d8 to your computer and use it in GitHub Desktop.
Don't judge the code... it suppose to be crap, this is just a very simple front controller I tend to use for quick mockups or playing with composer packages.
<?php
define('ROOT_DIR', __DIR__ . '/../');
define('VENDOR_DIR', ROOT_DIR. 'vendor/');
define('PUBLIC_DIR', ROOT_DIR . 'public/');
define('ROUTES_DIR', ROOT_DIR . 'routes/');
define('VIEW_DIR', ROOT_DIR . 'views/');
define('CACHE_DIR', ROOT_DIR . 'cache/');
// Include libs
require( VENDOR_DIR . 'autoloader.php' );
// Now lets sort out twig
$views = new Twig_Loader_Filesystem(VIEW_DIR);
// Add public dir for grabing assets
$views->addPath(PUBLIC_DIR);
$twig = new Twig_Environment($views, array(
'cache' => CACHE_DIR . 'twig',
'auto_reload' => true,
'strict_variables' => false,
'debug' => true
));
// Add debug functionality
$twig->addExtension(new Twig_Extension_Debug());
// Call any php function from within the templates
$twig->addFunction(
new Twig_SimpleFunction('php_*', function(){
$arg_list = func_get_args();
$function = array_shift($arg_list);
return call_user_func_array($function, $arg_list);
})
);
// Setup RedBeanPHP
R::setup('mysql:host=localhost;dbname=db', 'user', 'pass');
// Define data array
$data = array();
// Routes, all the routes!
$route = strtolower($_SERVER['REQUEST_URI']);
switch( $route ){
case '/who':
case '/technology':
case '/platforms':
case '/development':
case '/join-us':
case '/love-to-hear-from-you':
$data['route'] = substr($route, 1);
$view = $data['route'] . '.twig';
break;
case ( preg_match('/^\/news/', $route) ? true : false ):
$data['route'] = 'news';
$view = 'news.twig';
break;
case '/':
$data['route'] = 'index';
$view = 'index.twig';
break;
default:
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
$data['http_code'] = '404';
$data['route'] = 'error';
$view = 'error' . '.twig';
break;
}
// Do we have any route data to grab?
// These will also handle any POST/GET specific to the route
// Don't see the need of complicating this by making it non-procedural
if( isset($data['route']) ){
$routeController = ROUTES_DIR . $data['route'] . '.php';
if( file_exists($routeController) ){
include $routeController;
}
}
// POST/REDIRECT/GET
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
if( isset($_SESSION['message']) ){
$data['message'] = $_SESSION['message'];
unset($_SESSION['message']);
}
// That's it, now lets output the view
echo $twig->render($view, $data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment