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 understanding 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.
I'd like to get feedback to help improve my code.
Below is the full code for index.php, where my routing code is:
<?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 are 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
What would be the best way to set up "custom" URLs?
If I were to put in mywebsite.com/announcement
, it would check to see if it has controllers. Failing that, it would check to see if it has 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.