Skip to content

Instantly share code, notes, and snippets.

@codersatx
Created May 20, 2011 03:09
Show Gist options
  • Save codersatx/982276 to your computer and use it in GitHub Desktop.
Save codersatx/982276 to your computer and use it in GitHub Desktop.
The front controller for my portfolio that handles the loading of views based on the uri.
<?php
/*--------------------------------------------------------
Configurables
--------------------------------------------------------*/
$site_title = "San Antonio Web Designer and Developer - Alex Garcia";
$site_description = "The online web design and development portfolio of Alex Garcia.
A San Antonio based artist, web designer and web developer.";
$tracking_enabled = "false";
$site_offline = FALSE;
$page_not_found_view = "page-not-found.php";
$default_home_template = 'template-home.php';
$default_sidebar = 'sidebar-inner.php';
$default_inner_template = 'template-inner.php';
//Define our base app url
define("BASE_URL", 'http://work.garciamade.com/');
//Define where our views are located
define ("VIEWS", 'views/');
//Define our path seperator
define ("PATH_SEP", '-');
//If there is anything we need to remove from the beginning of the uri place it here.
define ("PATH_START", "");
define("PAGE_NOT_FOUND_VIEW", 'page-not-found.php');
/*--------------------------------------------------------
Non-Configurables
--------------------------------------------------------*/
if ($site_offline == TRUE)
{
die('Site is currently offline.');
}
else
{
//Get the current uri
$requested_page = $_SERVER['REQUEST_URI'];
//Replace anything we have in pathstarter
$requested_page = str_replace(PATH_START, "", $requested_page);
switch($requested_page)
{
case '/':
case '/home':
$requested_page = "home";
$template = $default_home_template;
$sidebar = $default_sidebar;
break;
default:
$template = $default_inner_template;
$sidebar = $default_sidebar;
break;
}
//Remove the trailing slash from uri so we don't get errors if the user
//enters page/name/ or page/name both will work.
$requested_page = rtrim($requested_page, "/");
//Replace the / with underscores and form our files we want to show
//The content page is the page with our content.
//we need to include an include($contentPage) in our view page.
$content_page = str_replace("/", PATH_SEP, $requested_page). ".php";
$content_page = ltrim($content_page, PATH_SEP);
$page_title = str_replace("-", " ", $content_page);
$page_title = ucwords(str_replace(".php", "", $page_title)) . " - " . $site_title;
//Check to see if the file exists if not throw an error
if ( ! file_exists(VIEWS . $content_page))
{
header("Status: 404");
$page_title = "404 Page Not Found" . " - " . $site_title;;
$template = "template-plain.php";
$content_page = PAGE_NOT_FOUND_VIEW;
include(VIEWS . $template);
}
else
{
include(VIEWS . $template);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment