Смотрите мой следующий пример.
index.php<?php
$aplication = "aplication";
$system = "system";
define("SYSPATH" , "./" . $system);
define("APPATH" , "./" . $aplication);
define("KEY" , true);
error_reporting(E_ALL | E_STRICT);
require SYSPATH . '/bootstrap.php';
echo $responceText;
system/bootstrap.php<?php if(!defined('KEY')) die('No Success');
require SYSPATH . '/classes/zzika.php';
require SYSPATH . '/classes/headers.php';
require SYSPATH . '/classes/content.php';
spl_autoload_register(array('Zzika', 'auto_load'));
ob_start();
$controller = Zzika::get_controller();
$controller = empty($controller) ? Zzika::config('default_controller'):$controller;
$controllerPath = APPATH . '/controllers/' . $controller . '.php';
if(file_exists($controllerPath))
{
require $controllerPath;
if(class_exists($controller))
{
$obj = new $controller;
echo $obj->index();
}
else
{
Zzika::error('Невозможно открыть контроллер: <b>'.$controller.'</b>');
}
}
else
{
Zzika::error('Невозможно открыть контроллер: <b>'.$controller.'</b>');
}
Zzika::get_headers();
Zzika::get_content();
$responceText = ob_get_contents();
ob_end_clean();
system/classes/zzika.php<?php if(!defined('KEY')) die('No Success');
class Zzika
{
public static $post;
public static $get;
function __construct()
{
self::$get = $_GET;
self::$post = $_POST;
}
public static function auto_load($class_name)
{
switch($class_name)
{
case "view":
require SYSPATH . '/classes/view.php';
break;
case "model":
require SYSPATH . '/classes/model.php';
break;
case "mysql":
require SYSPATH . '/classes/mysql.php';
break;
case "content":
require SYSPATH . '/classes/content.php';
break;
}
if(!class_exists($class_name))
{
Zzika::error('Класс ' . $class_name . ' не определен');
}
}
public static function get_controller()
{
if(empty(self::$get['url']))
{
$controller = Zzika::config('default_controller');
}
else
{
$match = explode("/", self::$get['url']);
require SYSPATH . '/config/url.php';
if(count($_url) > 0)
{
foreach($_url as $key=>$value)
{
if(preg_match($key, self::$get['url']))
{
$controller = $value;
}
}
}
else
{
$controller = $match[0];
}
}
return Zzika::url($controller);
}
public static function config($name_cfg, $cfg_type = 'config')
{
require SYSPATH . '/config/'.$cfg_type.'.php';
if(isset($_config[$name_cfg]))
{
return $_config[$name_cfg];
}
else if(isset($_database[$name_cfg]))
{
return $_database[$name_cfg];
}
}
public static function get_headers()
{
if(count(headers::$headers) > 0)
{
foreach(self::$headers as $k=>$v)
{
header($v);
}
}
}
public static function get_content()
{
$content = content::get();
if(!empty($content))
{
echo $content;
}
else
{
echo null;
}
}
public static function url($url)
{
$url = preg_replace("#\/#", "", $url);
$url = preg_replace("#\.#", "", $url);
$url = preg_replace("#\[#", "", $url);
$url = preg_replace("#\]#", "", $url);
$url = preg_replace("# #", "", $url);
return $url;
}
public static function error ($text)
{
echo $text;
die();
}
}
system/classes/content.php<?php if(!defined('KEY')) die('No Success');
class content
{
public static $content;
public static function message($text)
{
self::$content = self::$content . $text;
}
public static function get()
{
echo self::$content;
}
}
system/classes/headers.php<?php if(!defined('KEY')) die('No Success');
class headers
{
public static $headers;
public static function save($header)
{
self::$headers[] = $header;
}
}
system/classes/model.php<?php if(!defined('KEY')) die('No Success');
class model
{
public static function factory ($name, $variables = null)
{
if(!empty($variables))
{
extract($variables);
}
$model_path = APPATH . '/models/' . $name . '.php';
if(file_exists($model_path))
{
require $model_path;
$obj = new $name;
return $obj->index();
}
else
{
Zzika::error('Model is isset: ' . $name);
}
}
}
system/classes/view.php<?php if(!defined('KEY')) die('No Success');
class view
{
public static function factory ($name, $variables = null)
{
if(!empty($variables))
{
extract($variables);
}
$view_path = APPATH . '/views/' . $name . '.php';
if(file_exists($view_path))
{
$text = file_get_contents($view_path);
content::message(eval('?>'. $text));
}
else
{
content::message('View is empty ' . $view_path);
}
}
}
system/classes/mysql.php<?php if(!defined('KEY')) die('No Success');
class mysql
{
public static $connect;
public static function connect ()
{
self::$connect = mysql_connect(Zzika::config('mysql_host', 'database'), Zzika::config('mysql_user', 'database'), Zzika::config('mysql_password', 'database'));
if(!self::$connect)
{
Zzika::error('Невозможно соеденится с MySQL сервер');
}
}
public static function select_db()
{
if(mysql_select_db(Zzika::config('mysql_dbname', 'database'), self::$connect))
{
mysql_query("SELECT NAMES '".Zzika::config('mysql_charset', 'database')."'", self::$connect);
}
else
{
Zzika::error('Невозможно соеденится с БД: <b>'.Zzika::config('mysql_dbname', 'database').'</b>');
}
}
public static function query($query_string)
{
$result = mysql_query($query_string, self::$connect);
if($result === false)
{
$error = mysql_error();
$trace = debug_backtrace();
$head = $error ?'<b style="color:red">MySQL error: </b><br>
<b style="color:green">'. $error .'</b><br><br>':NULL;
$error_log = date("Y-m-d h:i:s") .' '. $head .'
<b>Query: </b><br>
<pre><span style="color:#CC0000">'. $trace[0]['args'][0] .'</pre></span><br><br>
<b>File: </b><b style="color:#660099">'. $trace[0]['file'] .'</b><br>
<b>Line: </b><b style="color:#660099">'. $trace[0]['line'] .'</b>';
file_put_contents(APPATH .'/logs/mysql.log', strip_tags($error_log) ."\n\n", FILE_APPEND);
header("HTTP/1.1 404 Not Found");
die("Произошла MySQL ошибка. Сообщение об ошибке оставлено на сервере и ошибка будет устранена.");
}
else
return $result;
}
}
system/config/config.php<?php if(!defined('KEY')) die("No success");
$_config = array(
'default_controller' =>'welcome'
);
system/config/url.php<?php if(!defined('KEY')) die('No Success');
$_url = array(
'#^[a-z0-9]+[/]{1}[a-zA-Z0-9_]+[\.]html$#' => 'news',
);
system/config/database.php<?php if(!defined('KEY')) die("No success");
$_database = array();
$_database['mysql_host'] = "localhost";
$_database['mysql_user'] = "root";
$_database['mysql_password'] = "";
$_database['mysql_dbname'] = "mvc";
$_database['mysql_dbprefix'] = "mvc_";
$_database['mysql_charset'] = "utf-8";
aplication/controllers/welcome.php<?php if(!defined('KEY')) die('No Success');
class welcome
{
function index ()
{
echo "<br>";
view::factory('www', array('www'=>'WWW http protocol')) . "<br />";
model::factory('mysqls');
}
}
aplication/models/mysqls.php<?php if(!defined('KEY')) die('No Success');
class mysqls
{
public function index()
{
mysql::connect();
mysql::select_db();
$query = mysql::query('SELECT * FROM `text`');
if(mysql_num_rows($query) > 0)
{
view::factory('mysql_result', array('myrow'=>$query));
}
else
{
view::factory('www', array('www'=>'Из бд ничего не выведено'));
}
}
}
aplication/views/mysql_result.php<?php if(!defined('KEY')) die("No success");
$i = 1;
while($myrows = mysql_fetch_assoc($myrow))
{
echo "<b>Text ".$i."</b><br>" . $myrows['text'] . "<br /><br />";
$i++;
}
aplication/views/www.php<?php if(!defined('KEY')) die('No Success');
echo $www . "<br>";
.htaccess остался такой же :D