Got router working
This commit is contained in:
parent
a91b2a414e
commit
17b8bdbc6b
@ -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
|
||||
|
@ -1,7 +1,20 @@
|
||||
<?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_controller' => 'test',
|
||||
'default_module' => 'default',
|
||||
'default_controller' => 'welcome',
|
||||
'default_module' => 'welcome',
|
||||
'404_route' => '',
|
||||
);
|
@ -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');
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
167
sys/common.php
167
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('<h1>404 Not Found</h1>');
|
||||
}
|
||||
|
||||
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');
|
||||
|
13
sys/db.php
13
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
|
||||
// End of db.php
|
||||
|
17
sys/page.php
17
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 = "<meta ";
|
||||
|
Loading…
Reference in New Issue
Block a user