I've created a method that grabs the content of the address bar, dissects it, and calls the correct class to be executed.
Read the comments above the method to follow the code.
Read the comments above the method to follow the code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Main { | |
/** | |
* Displays the desired page | |
* Grabs the $_GET['page'] variable and separates out the content | |
* Example of a link | |
* index.php?page=views-tire_shipments_item_number_packing_slips&type=0 | |
* $_GET['page'] is split at the dash (-) | |
* The first portion is the folder where the file is located | |
* The next portion is the file name and once the dashes are removed and | |
* each first letter capitalized, the class name is also generated. | |
* In this example, | |
* the folder would be: views | |
* the file_location would be: views/tire_shipments_item_number_packing_slips.php | |
* the class_name would be: TireShipmentsItemNumberPackingSlip | |
* | |
* Another example of a link might be | |
* index.php?page=__classes/login-Authorization | |
* In this case, the underscores are substituted with ../ to go up a directory | |
* In this example, | |
* the folder would be: ../../classes/login | |
* the file_location would be: ../../classes/login/Authorization.php | |
* the class_name would be: Authorization | |
* | |
* The code will check if the file exists. | |
* If so, it will include the file | |
* It proceeds to check if the class exists. | |
* If so, it will instantiate that class and call the main method | |
* Since I'm trying to have 1 class per page, each class will have the main method | |
* | |
* @return void | |
*/ | |
public function processRequest() { | |
if (!isset($_GET['page'])) { | |
$this->_index_view_obj->main(); | |
return; | |
} | |
$page = $_GET["page"]; | |
$page = explode("-", $page); | |
$folder = $page[0]; | |
$file = $page[1]; | |
// Checks to see if the folder has any underscores which means the file is located up a directory | |
// i.e. __classes means ../../classes | |
if (FALSE !== strpos($folder, "_")) { | |
$level_up = substr_count($folder, "_"); | |
$folder = str_replace("_", "", $folder); | |
for($i = 0; $i < $level_up; $i++) { | |
$folder = "../" . $folder; | |
} | |
} | |
$file_location = $folder . "/" . $file . ".php"; | |
$class_method = "main"; | |
// Capitalizes each word after an underscore | |
// i.e. tire_shipments_item_number_packing_slips will become | |
// TireShipmentsItemNumberPackingSlip | |
$class = preg_replace_callback('/(?:^|_)([a-z])/', | |
function ($m) { | |
return strtoupper($m[1]); | |
}, $file); | |
if (!file_exists($file_location)) { | |
$this->_error_reporting_obj->main(); | |
return; | |
} | |
require_once($file_location); | |
if (!class_exists($class)) { | |
$this->_error_reporting_obj->main(); | |
return; | |
} | |
$obj = new $class; | |
$obj->$class_method(); | |
} | |
} |
Comments
Post a Comment