【发布时间】:2013-05-26 19:36:24
【问题描述】:
我有点困惑。我想构建自己的框架只是为了了解一切是如何工作的,而不是将其用于大型项目。
我有一个 FrontController 类,这个类里面有路由功能。
- 设置/获取控制器参数的函数
- 从控制器设置/获取操作(方法)的函数
- 解析请求的 URI 并返回适当的控制器(如果存在)的函数,如果不存在则返回默认控制器,即 IndexController。
-
Run() 方法执行以下操作:
public function run() { $method = new \ReflectionMethod($this->controller, $this->action); $numParams = $method->getNumberOfParameters(); //fill missing parameters with null values if (count($this->params) != $numParams) { $tempArray = array_fill(0, $numParams, null); $this->setParams($this->params + $tempArray); } $controller = new $this->controller; $userInstance = User::getInstance(); //just creates a model based on the controller name by default its //Index.php (model) $model = DB::createModel($this->getControllerName()); //run _before before any function $controller->_before($model, $userInstance); call_user_func_array(array($controller, $this->action), $this->params); return; }现在我看过教程,他们使用
BaseController,然后每个控制器都从这个basecontroller扩展。我的控制器没有从 FrontController 扩展。
我的问题是我需要单独的路由类吗?我需要将 FrontController 拆分成
- 基础控制器
- Route.php
- 模型.php
由于 run() 函数实际上将模型和用户对象传递给控制器。
【问题讨论】: