Zrobiłem go na takiej zasadzie:
struktura folderów i plików:
.htaccess index.php [folder]conf Dispatcher.php _init.php Model.php Database.php Autoloader.php [folder]lib: debugClass.php [folder]controllers: [folder]index: Controller.php [folder]cache
Plik .htaccess - zadanie kierowania wszelkiego ruchu do index.php:
RewriteEngine On # Przekierowanie całego ruchu do index.php RewriteRule (.*) index.php [L]
Plik index.php - załączenie frameworka z innego katalogu:
<?php // Uruchamienie aplikacji require_once('system/_init.php'); // Wyłączanie parsera
system/_init.php - główny plik frameworku:
<?php // Definicje // Autoloader require(INC_PATH.'system/Autoloader.php'); // Debug $debug = new Debug; // Załaczanie niezbędnych plików $debug->drequire(INC_PATH.'system/Dispatcher.php'); $debug->drequire(INC_PATH.'system/Model.php'); $debug->drequire(INC_PATH.'system/Database.php'); // Uruchamianie $dispatcher = new Dispatcher; $dispatcher->_init();
system/Dispatcher.php - przetwarza ścieżkę wpisaną przez usera:
<?php class Dispatcher { private $_strUrlParameters; private $_arrUrlParameters; private $_index = FALSE; public function __construct() { { $strUrlParameters = $_SERVER['PATH_INFO']; } else { $strUrlParameters = $_SERVER['REQUEST_URI']; } } public function _init() { $this->_parsePath(); $strControllerName = $this->_getControllerName(); $strControllerFile = INC_PATH.'controllers/'.$this->_getControllerName().'/Controller.php'; { require_once($strControllerFile); $strControllerClasName = $this->_getControllerName().'Controller'; $objController = new $strControllerClasName($this->_getParametersForController()); $objController->_init(); } elseif ($this->index == FALSE) { require_once(INC_PATH.'controllers/index/Controller.php'); $objController = new indexController($this->_getParametersForController()); $objController->_init(); } } private function _parsePath() { $this->_arrUrlParameters = $arrParameters; } private function _getControllerName() { $strControllerName = $this->_arrUrlParameters[0]; return $strControllerName; elseif ($this->index == FALSE) { $this->index = TRUE; require_once(INC_PATH.'controllers/index/Controller.php'); $objController = new indexController($this->_getParametersForController()); $objController->_init(); } } private function _getParametersForController() { for($i = 1; $i < $intNumOfPathParameters; $i++) $arrPrametersForController[] = $this->_arrUrlParameters[$i]; return $arrPrametersForController; } }
system/Model.php - plik pusty, jeszcze nic tam nie wrzuciłem.
system/Database.php - sterownik bazy danych.
<?php class Database { public $server; public $answer; var $rows; var $queries = 0; var $cache_state = 0; var $cache_file; var $cache_buffer; var $cache_ptr; public function CONNECT ($HOST,$USER,$PASSWORD,$DATABASE) { $this->server = @mysql_connect ($HOST,$USER,$PASSWORD) OR die ('dbClass.php ERROR: '.mysql_error()); $selectDatabase = @mysql_select_db ($DATABASE, $this->server) OR die ('dbClass.php ERROR: '.mysql_error()); } public function POLISH_CHARACTERS () { } public function SELECT ($query) { return $this->answer = $array; } public function VAILD ($query) { if ($qty >= 1) $return = TRUE; else $return = FALSE; return $this->answer = $return; } public function QUERY ($query) { return $this->answer = $answer; } public function VAILD_QUERY ($query) { else $return = FALSE; return $this->answer = $return; } public function sql_cache($handle = 0) { $this->cache_state = 1; $this->cache_ptr = 0; } else { $this->cache_state = 2; $this->cache_file = CACHE_DIR.'sql_cache-'.$handle.'.cache'; } } else { if($this->cache_state == 2) { } $this->cache_state = 0; } } function sql_cache_remove($handle) { } } function cache_query($query) { if($this->cache_state != 1) { return 1; } } function sql_fetch_array() { if($this->cache_state == 1) { return 0; } $this->rows = $this->cache_buffer[$this->cache_ptr]; $this->cache_ptr++; return 1; } else { if($this->cache_state == 2) { $this->cache_buffer[] = $this->rows; } return 1; } } return 0; } function sql_fetch_row() { if($this->cache_state == 1) { return 0; } $this->rows = $this->cache_buffer[$this->cache_ptr]; $this->cache_ptr++; return 1; } else { if($this->cache_state == 2) { $this->cache_buffer[] = $this->rows; } return 1; } } return 0; } } ?>
system/Autoloader.php - ładuje biblioteki z folderu:
<?php function __autoload ($classname) { require_once(INC_PATH.'lib/'.$classname.'Class.php'); }
lib/debugClass.php - liczy jak długo parseruje się dana klasa:
<?php function getMicrotime() { return ((float)$usec + (float)$sec); } class Debug { public function drequire($dir) { $this->requireArray[$dir] = getMicrotime(); require_once($dir); $this->requireArray[$dir] = getMicrotime()-$this->requireArray[$dir]; } public function RequireTime () { return $this->requireArray; } }
contollers/index/Controller.php - plik kontrolera index:
<?php class indexController { var $var; function __CONSTRUCT ($var) { $this->var = $var[0]; } public function _init() { if ($this->var == "test") elseif ($this->var == "debug") { } else } }
Prosiłbym o rady takie, które odpowiedzą mi na pewne pytania:
- Czy dobrze zbudowałem cały framework? Coś powinienem zmienić?
- Mam taki problem, że gdy chce np. wykonać akcje localhost/index/debug to dostaje błąd, że $debug nie jest obiektem. Wiem o tym, ale jak stworze go na nowo, to nie będzie miał już tej tablicy z danymi. Jak to rozmwiać?
- Inne uwagi według własnego uznania mile widziane.