I am in the progress of creating a MVC structured website. I've opted to make my own instead of using pre-constructed MVC systems as a way to teach myself better coding and understand how applications work. I've got everything laid out but I'm not happy with my routing system - it is VERY crude and I'd like it to be more robust. Below is the full code for index.php where my routing code is, I'd like to get feed back and improve my code.
<?php
session_start();
// Estabilish Database Connections and system defaults
include('config.php');
function setReporting() {
if (DEVELOPMENT_ENVIRONMENT == true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
ini_set('error_log', ROOT.DS.'tmp'.DS.'logs'.DS.'error.log');
}
}
function db_connect() {
$connection = mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
if (!$connection) {
die("<h2>Error Connecting to Database</h2>");
}
if(!mysql_select_db(DATABASE, $connection)) {
die("<h2>Database Does Not Exist</h2>");
}
return $connection;
}
function hook() {
$params = parse_params();
$url = $_SERVER['REQUEST_URI'];
$url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $url);
$urlArray = array();
$urlArray = explode("/",$url);
var_dump($urlArray);
if (isset($urlArray[2]) & !empty($urlArray[2])) {
$route['controller'] = $urlArray[2];
} else {
$route['controller'] = 'front'; // Default Action
}
if (isset($urlArray[3]) & !empty($urlArray[3])) {
$route['view'] = $urlArray[3];
} else {
$route['view'] = 'index'; // Default Action
}
include(CONTROLLER_PATH.$route['controller'].'.php');
include(VIEW_PATH.$route['controller'].DS.$route['view'].'.php');
var_dump($route['controller']);
var_dump($route['view']);
var_dump($urlArray);
var_dump($params);
// reseting messages
$_SESSION['flash']['notice'] = '';
$_SESSION['flash']['warning'] = '';
}
// Return form array
function parse_params() {
$params = array();
if(!empty($_POST)) {
$params = array_merge($params, $_POST);
}
if(!empty($_GET)) {
$params = array_merge($params, $_GET);
}
return $params;
}
// Prepare General Application Models
require($_SERVER['DOCUMENT_ROOT'].'/'.APP.'/'.'models/'.'general.php');
setReporting();
date_default_timezone_set(get_timezone());
$current_theme = current_theme();
hook();
if($_SESSION['flash']['notice']) {
echo $_SESSION['flash']['notice'];
}
One big problem is that my hook call won't work properly when the urls two levels deep. Say I wanted to visit mywebsite.com/admin it would work, however mywebsite.com/admin/dashboard would not. The problem is in the arrays, how could I get the array to load content after the 2nd level along with the second level.
Would it be best to create an array like this?
Array
- controller
- view
- dashboard
Any help here would be great. Also as a side question. What would be the best way to set up "custom" urls. So if I were to put in mywebsite.com/announcement it would check to see if its got controllers, failing that, check to see if it's got custom content (maybe a file of the same name in "customs" folder, and then if there's nothing execute the 404 page not found stuff) This isn't a priority question though, but loosely associated in how the code works so I thought it best to add.
UPDATE
<?php
session_start();
// Estabilish Database Connections and System Defaults
include('config.php');
function setReporting() {
error_reporting(E_ALL);
ini_set('display_errors', DEVELOPMENT_ENVIRONMENT);
ini_set('log_errors', 'On');
}
function dbConnect() {
$connection = mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
if (!$connection) {
die("<h2>Error Connecting to Database</h2>");
}
if(!mysql_select_db(DATABASE, $connection)) {
die("<h2>Database Does Not Exist</h2>");
}
return $connection;
}
// Return form array
function parse_params() {
$params = array();
if(!empty($_POST)) {
$params = array_merge($params, $_POST);
}
if(!empty($_GET)) {
$params = array_merge($params, $_GET);
}
return $params;
}
function hook() {
$params = parse_params();
$url = $_SERVER[ 'REQUEST_URI' ];
$url = trim( $url, '/' );//remove forward slash from beginning and end of $url
$urlArray = explode( '/', $url );
$urlArray = array_slice($urlArray, 1);
$urlArray = array_pad( $urlArray, 2, NULL);
list( $controller, $view ) = $urlArray;
if( ! $controller ) { $controller = 'front'; }
if( ! $view ) { $view = 'index'; }
$route = compact( 'controller', 'view' );
include(CONTROLLER_PATH.$route['controller'].'.php');
include(VIEW_PATH.$route['controller'].DS.$route['view'].'.php');
}
// Prepare General Application Models
require($_SERVER['DOCUMENT_ROOT'].'/'.APP.'/'.'models/'.'general.php');
setReporting();
date_default_timezone_set(get_timezone());
hook();