1. index
<?php /*** error reporting on ***/ error_reporting(E_ALL); /*** define the site path ***/ $site_path = realpath(dirname(__FILE__)); define ('__SITE_PATH', $site_path); /*** include the init.php file ***/ include 'includes/init.php'; /*** load the router ***/ $registry->router = new router($registry); /*** set the controller path ***/ $registry->router->setPath (__SITE_PATH . '/controller'); /*** load up the template ***/ $registry->template = new template($registry); /*** load the controller ***/ $registry->router->loader(); ?>2. ini
<?php /*** include the controller class ***/ include __SITE_PATH . '/application/' . 'controller_base.class.php'; /*** include the registry class ***/ include __SITE_PATH . '/application/' . 'registry.class.php'; /*** include the router class ***/ include __SITE_PATH . '/application/' . 'router.class.php'; /*** include the template class ***/ include __SITE_PATH . '/application/' . 'template.class.php'; /*** auto load model classes ***/ function __autoload($class_name) { $filename = strtolower($class_name) . '.class.php'; $file = __SITE_PATH . '/model/' . $filename; if (file_exists($file) == false) { return false; } include ($file); } /*** a new registry object ***/ $registry = new registry; /*** create the database registry object ***/ // $registry->db = db::getInstance(); ?>3. registry
<?php Class Registry { /* * @the vars array * @access private */ private $vars = array(); /** * * @set undefined vars * * @param string $index * * @param mixed $value * * @return void * */ public function __set($index, $value) { $this->vars[$index] = $value; } /** * * @get variables * * @param mixed $index * * @return mixed * */ public function __get($index) { return $this->vars[$index]; } } ?>以上把他們當成第一組,index定義路徑、呼叫ini,在ini中在建立一個registy的物件如同登錄檔一樣紀錄每一個物件
後呼叫router的Load()方法,
4. router
<?php class router { /* * @the registry */ private $registry; /* * @the controller path */ private $path; private $args = array(); public $file; public $controller; public $action; function __construct($registry) { $this->registry = $registry; } /** * * @set controller directory path * * @param string $path * * @return void * */ function setPath($path) { /*** check if path i sa directory ***/ if (is_dir($path) == false) { throw new Exception ('Invalid controller path: `' . $path . '`'); } /*** set the path ***/ $this->path = $path; } /** * * @load the controller * * @access public * * @return void * */ public function loader() { /*** check the route ***/ $this->getController(); /*** if the file is not there diaf ***/ if (is_readable($this->file) == false) { $this->file = $this->path.'/error404.php'; $this->controller = 'error404'; } /*** include the controller ***/ include $this->file; /*** a new controller class instance ***/ $class = $this->controller . 'Controller'; $controller = new $class($this->registry); /*** check if the action is callable ***/ if (is_callable(array($controller, $this->action)) == false) { $action = 'index'; } else { $action = $this->action; } /*** run the action ***/ $controller->$action(); } /** * * @get the controller * * @access private * * @return void * */ private function getController() { /*** get the route from the url ***/ $route = (empty($_GET['rt'])) ? '' : $_GET['rt']; if (empty($route)) { $route = 'index'; } else { /*** get the parts of the route ***/ $parts = explode('/', $route); $this->controller = $parts[0]; if(isset( $parts[1])) { $this->action = $parts[1]; } } if (empty($this->controller)) { $this->controller = 'index'; } /*** Get action ***/ if (empty($this->action)) { $this->action = 'index'; } /*** set the file path ***/ $this->file = $this->path .'/'. $this->controller . 'Controller.php'; } } ?>5. indexController
<?php Class indexController Extends baseController { public function index() { /*** set a template variable ***/ $this->registry->template->welcome = 'Welcome to PHPRO MVC'; /*** load the index template ***/ $this->registry->template->show('index'); } } ?>6. blogController
<?php Class blogController Extends baseController { public function index() { $this->registry->template->blog_heading = 'This is the blog Index'; $this->registry->template->show('blog_index'); } public function view(){ /*** should not have to call this here.... FIX ME ***/ $this->registry->template->blog_heading = 'This is the blog heading'; $this->registry->template->blog_content = 'This is the blog content'; $this->registry->template->show('blog_view'); } } ?>這三個為第二組,ROUTER的Load()會根據網址GET的內容而導向不同的檔案。blogController即indexController中定義模板用的變數及呼叫出模板。
7. template
<?php Class Template { /* * @the registry * @access private */ private $registry; /* * @Variables array * @access private */ private $vars = array(); /** * * @constructor * * @access public * * @return void * */ function __construct($registry) { $this->registry = $registry; } /** * * @set undefined vars * * @param string $index * * @param mixed $value * * @return void * */ public function __set($index, $value) { $this->vars[$index] = $value; } function show($name) { $path = __SITE_PATH . '/views' . '/' . $name . '.php'; if (file_exists($path) == false) { throw new Exception('Template not found in '. $path); return false; } // Load variables foreach ($this->vars as $key => $value) { $$key = $value; } include ($path); } } ?>8. index
<h1><?php echo $welcome; ?></h1>9. blog_index
<h1><?php echo $blog_heading; ?></h1>10. blog_view
<h1><?php echo $blog_heading; ?></h1> <p><?php echo $blog_content; ?></p>第三組這四個檔案,由前述第2組導向index、blog_index、blog_view並將變數帶入模板中,而其用的方法跟registry類別一樣使用magic method。
請參閱:http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
update:2010/3/27
沒有留言:
張貼留言