diff --git a/app/config/config.php b/app/config/config.php index e01f400..6c3eca0 100644 --- a/app/config/config.php +++ b/app/config/config.php @@ -20,6 +20,16 @@ define('SHOW_DEBUG_BACKTRACE', TRUE); */ define('BASE_URL', $default_baseurl); +/* +|-------------------------------------------------------------------------- +| Url index file +|-------------------------------------------------------------------------- +| +| This determines whether "index.php" is in generated urls +| +*/ +define('URL_INDEX_FILE', 'index.php/'); + /* |-------------------------------------------------------------------------- | Content Domain diff --git a/app/config/routes.php b/app/config/routes.php index 7c6ddb6..75675ed 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -1,7 +1,20 @@ 'blog/blog/index' + * + * To route a special 404 page, set '404_route' to the "module/controller/method" you wish to use + * + */ + +return array( // Default Paths - 'default_controller' => 'test', - 'default_module' => 'default', + 'default_controller' => 'welcome', + 'default_module' => 'welcome', + '404_route' => '', ); \ No newline at end of file diff --git a/app/errors/error.php b/app/errors/error_php.php similarity index 100% rename from app/errors/error.php rename to app/errors/error_php.php diff --git a/index.php b/index.php index 0adeb1c..91f3fd9 100644 --- a/index.php +++ b/index.php @@ -8,7 +8,10 @@ define('SYS_PATH', __DIR__.'/sys/'); define('MOD_PATH', __DIR__.'/modules/'); define('APP_PATH', __DIR__.'/app/'); -$default_baseurl = "//".$_SERVER['HTTP_HOST']. str_replace("index.php", "", $_SERVER['REQUEST_URI']); +$ri = $_SERVER['REQUEST_URI']; +$ind_pos = stripos($ri, "index.php/"); +$default_path = ($ind_pos !== FALSE) ? substr($ri, 0, $ind_pos) : $ri; +$default_baseurl = "//".$_SERVER['HTTP_HOST']. $default_path; // Require the basic configuratio file require(APP_PATH.'config/config.php'); diff --git a/modules/welcome/controllers/welcome.php b/modules/welcome/controllers/welcome.php index efd997b..93362af 100644 --- a/modules/welcome/controllers/welcome.php +++ b/modules/welcome/controllers/welcome.php @@ -10,11 +10,17 @@ class Welcome extends MM_Controller { function index() { - $this->page->build_header(); + $this->page->output_string($this->page->set_message('info', "This is just a test message", TRUE)); + } + + function php() + { + ob_start(); + phpinfo(); + $output = ob_get_contents(); + ob_end_clean(); - $this->page->set_message('info', "This is just a test message"); - //$this->output->append_output($this->__toString()); - $this->page->build_footer(); + $this->output->set_output($output); } } \ No newline at end of file diff --git a/sys/common.php b/sys/common.php index 18f7b99..f3231a1 100644 --- a/sys/common.php +++ b/sys/common.php @@ -27,24 +27,6 @@ function load_file($file, $curr_path="") { $path = MOD_PATH."{$curr_path}/{$file}.php"; } - - if( ! is_file($path)) - { - /*if($curr_path !== "") - { - $matches = array(); - if(preg_match("`modules/([a-z 0-9~%.:_\-])/?`i", $curr_path, $matches)) - { - $module = $matches[1]; - - $path = MOD_PATH."{$module}/{$file}.php"; - } - } - else - {*/ - $path = MOD_PATH."welcome/{$file}.php"; - //} - } if(is_file($path)) { @@ -52,33 +34,6 @@ function load_file($file, $curr_path="") } } -function load_class($name, &$self=array()) -{ - - if(is_array($self)) - { - $self =& miniMVC::get_instance(); - } - - $path = SYS_PATH."{$name}.php"; - $class = "{$name}"; - - if(class_exists($class, FALSE)) - { - if( ! isset($self->$name)) - { - $self->$name = new $class; - return; - } - } - - load_file($name, 'sys'); - - if(class_exists($class, FALSE)) - { - $self->$name = new $class; - } -} // -------------------------------------------------------------------------- @@ -107,7 +62,7 @@ function on_error($severity, $message, $filepath, $line, $context) // Contain the content for buffering ob_start(); - include(APP_PATH.'/errors/error.php'); + include(APP_PATH.'/errors/error_php.php'); $buffer = ob_get_contents(); ob_end_clean(); @@ -158,6 +113,17 @@ function on_exception($exception) echo $msg; } +function show_404() +{ + @header('HTTP/1.1 404 Not Found', TRUE, 404); + die('

404 Not Found

'); +} + +function show_error() +{ + +} + // -------------------------------------------------------------------------- /** @@ -165,25 +131,118 @@ function on_exception($exception) */ function route() { - $controller = "welcome"; - $module = "welcome"; - $func = "index"; + $pi = $_SERVER['PATH_INFO']; - if( ! empty($_SERVER['PATH_INFO'])) + // Load the routes config file + $routes = require_once(APP_PATH.'config/routes.php'); + + // Set the default route + $module = $routes['default_module']; + $controller = $routes['default_controller']; + $func = "index"; + $route_set = FALSE; + + + if( ! empty($pi) && $pi !== "/") { - $segments = explode('/', $_SERVER['PATH_INFO']); + //Remove trailing slash and begining slash + $pi = trim($pi, '/'); + + // URL matches the route exactly? Cool, that was easy + if(isset($routes[$pi])) + { + list($module, $controller, $func) = explode("/", $routes[$pi]); + $route_set = TRUE; + } + else + { + $custom_routes = $routes; + + // Skip required routes + unset($custom_routes['default_module']); + unset($custom_routes['default_controller']); + unset($custom_routes['404_handler']); + + foreach($custom_routes as $uri => $map) + { + if(preg_match("`{$uri}`i", $pi)) + { + list($module, $controller, $func) = explode("/", $map); + $route_set = TRUE; + break; + } + } + } + + // Doesn't match a predefined route? + // Match on module/controller/method, controller/method, or method + if( ! $route_set) + { + if(strpos($pi, '/') === FALSE) + { + $num_segments = 1; + } + else + { + $segments = explode('/', $pi); + $num_segments = count($segments); + } + + + if($num_segments === 1) + { + $func = $pi; + } + + if($num_segments === 2) + { + list($controller, $func) = $segments; + } + + if($num_segments >= 3) + { + list($module, $controller, $func) = $segments; + } + } } - load_file("controllers/{$controller}", $module); + $path = MOD_PATH."{$module}/controllers/{$controller}.php"; - $class = new $controller; + if(is_file($path)) + { + require_once($path); + + $methods = get_class_methods($controller); + + if(in_array($func, $methods)) + { + $class = new $controller; - call_user_func(array($class, $func)); + return call_user_func(array($class, $func)); + } + + // Function doesn't exist…404 + show_404(); + } + + // If it gets here, it's still a 404 + show_404(); } // -------------------------------------------------------------------------- +/** + * Returns a full url from a url segment + * + * @param string $segment + * @return string + */ +function site_url($segment) +{ + return $url = BASEURL . URL_INDEX_FILE . $segment; +} +// -------------------------------------------------------------------------- //Set error handlers set_error_handler('on_error'); diff --git a/sys/db.php b/sys/db.php index 6da2bb5..7d82d67 100644 --- a/sys/db.php +++ b/sys/db.php @@ -7,13 +7,12 @@ class db extends PDO { private $where; private static $instance; - public static function get_instance() + public static function get_instance($dbname="default") { if ( ! isset(self::$instance)) { echo 'Creating new instance of db class.'; - $className = __CLASS__; - self::$instance = new $className; + self::$instance = new db($dbname); } return self::$instance; @@ -125,10 +124,12 @@ class db extends PDO { * * @param array $members */ - function __invoke($members=array()) + function __invoke($db="default") { - return self::$instance; + return self::get_instance($db); } + + } -// End of db.php \ No newline at end of file +// End of db.php diff --git a/sys/page.php b/sys/page.php index 2ec4701..1b382dc 100644 --- a/sys/page.php +++ b/sys/page.php @@ -474,6 +474,23 @@ class Page // -------------------------------------------------------------------------- + /** + * Output String + * + * Similar to render(), this is a shortcut + * to output a string in the body of the + * page. + * @param string $string + */ + function output_string($string) + { + $this->build_header(); + $this->mm->output->append_output($string); + $this->build_footer(); + } + + // -------------------------------------------------------------------------- + private function _meta($params) { $string = "