【发布时间】:2012-11-07 08:55:12
【问题描述】:
我正在尝试通过前端控制器设计深入研究 MVC。
我想通过一行来调用我的整个应用程序,例如在 index.php 中:
require_once(myclass.php);
$output = new myClass();
我很想摆脱 require_once 行,但我不明白如何在不包含它的情况下加载我的类?
无论如何,我的主要问题是如何使用一个前端类加载我的各种控制器、模型和视图等。到目前为止,我想出了:
class myClass
{
private $name;
public $root = $_SERVER['DOCUMENT_ROOT'];
private $route = array("Controller" => "", "Model" => "", "View" => "", "Paremeters" => "");
function __construct()
{ $uri = explode("/",$_SERVER['REQUEST_URI']);
if(isset($uri[2])) {$this->route['Controller'] = $uri[2];}
if(isset($uri[3])) {$this->route['Model'] = $uri[3];}
if(isset($uri[4])) {$this->route['View'] = $uri[4];}
if(isset($this->route['Controller']))
{
include($this->root."/".$this->route['Controller'].".php");
}
}
}
但它似乎有点令人费解而且过于夸张。另外,一旦我在 __construct 中包含了新类,我应该如何加载它?
对于知识的缺乏,我深表歉意,我已经用谷歌搜索了很多次,但我不断找到相同的页面,似乎并没有扩展我对此事的了解。
【问题讨论】:
-
如果你想摆脱
require,请使用autoloading -
但是我应该把自动加载功能放在哪里呢?我希望我的索引文件只是 $ouput = new myClass(),它怎么知道在 myClass.php 中查找自动加载?
-
您还可以查看现有 mvc 框架的代码。例如Kohana
标签: php model-view-controller front-controller