/
index.php
69 lines (52 loc) · 2.56 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
session_start();
define( 'APP_ROOT', dirname( __FILE__ ) . '/protected' );
// Get CoughPHP
require_once( APP_ROOT . '/vendor/coughphp/load.inc.php' );
require_once( APP_ROOT . '/vendor/coughphp/as_database/load.inc.php' );
require_once( APP_ROOT . '/vendor/coughphp/extras/Autoloader.class.php');
Autoloader::addClassPath( APP_ROOT . '/models/' );
Autoloader::setCacheFilePath( APP_ROOT . '/cache/cough_class_path_cache.txt');
spl_autoload_register( array( 'Autoloader', 'loadClass' ) );
// Get config stuff
require_once( APP_ROOT . '/config/config.php' );
require_once( APP_ROOT . '/config/routes.php' );
require_once( APP_ROOT . '/config/database.php' );
// Get system classes
require_once( APP_ROOT . '/system/view.php' );
require_once( APP_ROOT . '/system/controller.php' );
require_once( APP_ROOT . '/system/uri.php' );
// Parse URI
if( false === strpos( $_SERVER['REQUEST_URI'], $config['uri_prefix'] ) )
die( 'Configuration Error - Bad uri_prefix' );
$path = substr( $_SERVER['REQUEST_URI'], strlen( $config['uri_prefix'] ) );
$components = explode( '/', $path );
for( $i = 0; $i < count( $components ); ++$i )
if( empty( $components[$i] ) )
unset( $components[$i] );
$controller_name = $config['default_controller'];
$method_name = 'index';
$arguments = array();
if( 1 <= count( $components ) ) { $controller_name = strtolower( $components[0] ); }
if( 2 <= count( $components ) ) { $method_name = strtolower( $components[1] ); }
if( 3 <= count( $components ) ) { $arguments = array_slice( $components, 2 ); }
// Routing...
if( array_key_exists( $controller_name, $routes ) && array_key_exists( $method_name, $routes[$controller_name] ) ) {
$n_controller_name = $routes[$controller_name][$method_name][0];
$n_method_name = $routes[$controller_name][$method_name][1];
$controller_name = $n_controller_name;
$method_name = $n_method_name;
}
$controller_file = APP_ROOT . '/controllers/' . $controller_name . '.php';
$controller_class = $controller_name . '_Controller';
if( ! file_exists( $controller_file ) )
die( "Bad Controller: $controller_name" );
require_once( $controller_file );
if( ! class_exists( $controller_class ) )
die( "Bad Controller: $controller_name" );
$controller_obj = new $controller_class();
if( ! is_callable( array( $controller_obj, $method_name ) ) )
die( "Bad Method for $controller_name: $method_name" );
if( false === call_user_func_array( array( &$controller_obj, $method_name ), $arguments ) )
die( "Error Calling Method for $controller_name: $method_name" );
die( $controller_obj->render() );