Got router working

This commit is contained in:
Timothy Warren 2011-12-29 16:57:28 -05:00
parent a91b2a414e
commit 17b8bdbc6b
8 changed files with 177 additions and 68 deletions

View File

@ -20,6 +20,16 @@ define('SHOW_DEBUG_BACKTRACE', TRUE);
*/ */
define('BASE_URL', $default_baseurl); 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 | Content Domain

View File

@ -1,7 +1,20 @@
<?php <?php
$routes = array( /**
* File to configure routes
*
* Routes work on simple/regex matching.
*
* For a route mapping [http://example.com/]blog to the blog controller in the blog module:
* 'blog' => '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 Paths
'default_controller' => 'test', 'default_controller' => 'welcome',
'default_module' => 'default', 'default_module' => 'welcome',
'404_route' => '',
); );

View File

@ -8,7 +8,10 @@ define('SYS_PATH', __DIR__.'/sys/');
define('MOD_PATH', __DIR__.'/modules/'); define('MOD_PATH', __DIR__.'/modules/');
define('APP_PATH', __DIR__.'/app/'); 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 the basic configuratio file
require(APP_PATH.'config/config.php'); require(APP_PATH.'config/config.php');

View File

@ -10,11 +10,17 @@ class Welcome extends MM_Controller {
function index() function index()
{ {
$this->page->build_header(); $this->page->output_string($this->page->set_message('info', "This is just a test message", TRUE));
}
$this->page->set_message('info', "This is just a test message"); function php()
//$this->output->append_output($this->__toString()); {
$this->page->build_footer(); ob_start();
phpinfo();
$output = ob_get_contents();
ob_end_clean();
$this->output->set_output($output);
} }
} }

View File

@ -28,57 +28,12 @@ function load_file($file, $curr_path="")
$path = MOD_PATH."{$curr_path}/{$file}.php"; $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)) if(is_file($path))
{ {
require_once($path); require_once($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 // Contain the content for buffering
ob_start(); ob_start();
include(APP_PATH.'/errors/error.php'); include(APP_PATH.'/errors/error_php.php');
$buffer = ob_get_contents(); $buffer = ob_get_contents();
ob_end_clean(); ob_end_clean();
@ -158,6 +113,17 @@ function on_exception($exception)
echo $msg; echo $msg;
} }
function show_404()
{
@header('HTTP/1.1 404 Not Found', TRUE, 404);
die('<h1>404 Not Found</h1>');
}
function show_error()
{
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
@ -165,25 +131,118 @@ function on_exception($exception)
*/ */
function route() function route()
{ {
$controller = "welcome"; $pi = $_SERVER['PATH_INFO'];
$module = "welcome";
$func = "index";
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);
call_user_func(array($class, $func)); $methods = get_class_methods($controller);
if(in_array($func, $methods))
{
$class = new $controller;
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 handlers
set_error_handler('on_error'); set_error_handler('on_error');

View File

@ -7,13 +7,12 @@ class db extends PDO {
private $where; private $where;
private static $instance; private static $instance;
public static function get_instance() public static function get_instance($dbname="default")
{ {
if ( ! isset(self::$instance)) if ( ! isset(self::$instance))
{ {
echo 'Creating new instance of db class.'; echo 'Creating new instance of db class.';
$className = __CLASS__; self::$instance = new db($dbname);
self::$instance = new $className;
} }
return self::$instance; return self::$instance;
@ -125,10 +124,12 @@ class db extends PDO {
* *
* @param array $members * @param array $members
*/ */
function __invoke($members=array()) function __invoke($db="default")
{ {
return self::$instance; return self::get_instance($db);
} }
} }
// End of db.php // End of db.php

View File

@ -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) private function _meta($params)
{ {
$string = "<meta "; $string = "<meta ";