几年来我一直在编写程序,最近我决定跳到面向对象的代码。
为了帮助我站稳脚跟,我一直在开发自己的 MVC 框架。我知道 Zend 等等,但我只想要一些优雅和轻量级的东西,我可以 100% 理解所有内容并且可以积累知识。但是,我需要一点帮助和建议。
基本文件夹架构是:
/view/
/controller/
/model/
index
.htaccess
这些是我到目前为止的文件:
/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
/index.php
//autoload new classes
function __autoload($class)
{
$folder = explode('_', $class);
require_once strtolower(str_replace('_', '/', $class)).'_'.$folder[0].'.php';
}
//instantiate controller
if (!isset($_GET['controller'])) { $_GET['controller'] = 'landing'; }
$controller_name = 'controller_'.$_GET['controller'];
new $controller_name($_GET,$_POST);
/controller/base_controller.php
abstract class controller_base
{
//store headers
protected $get;
protected $post;
//store layers
protected $view;
protected $model;
protected function __construct($get,$post)
{
//store the header arrays
$this->get = $get;
$this->post = $post;
//preset the view layer as an array
$this->view = array();
}
public function __destruct()
{
//extract variables from the view layer
extract($this->view);
//render the view to the user
require_once('view/'.$this->get['controller'].'_view.php');
}
}
/controller/landing_controller.php
class controller_landing extends controller_base
{
public function __construct($get,$post)
{
parent::__construct($get,$post);
//simple test of passing some variables to the view layer
$this->view['text1'] = 'some different ';
$this->view['text2'] = 'bit of text';
}
}
问题 1) 这个框架的布局是否正确?
问题2)我应该如何将模型层集成到这个框架中?
问题 3) 关于如何改进这一点的任何其他建议?