##opencart code review
mvc框架,同YII之类php web框架。
整个流程就在index.php中
###流程
(1)system/startup.php
load需要的php
// Engine
require_once(modification(DIR_SYSTEM . 'engine/action.php'));
require_once(modification(DIR_SYSTEM . 'engine/controller.php'));
require_once(modification(DIR_SYSTEM . 'engine/event.php'));
require_once(modification(DIR_SYSTEM . 'engine/front.php'));
require_once(modification(DIR_SYSTEM . 'engine/loader.php'));
require_once(modification(DIR_SYSTEM . 'engine/model.php'));
require_once(modification(DIR_SYSTEM . 'engine/registry.php'));
// Helper
require_once(DIR_SYSTEM . 'helper/json.php');
require_once(DIR_SYSTEM . 'helper/utf8.php');
(2)创建Registry对象
一个词典类key-value,把其他类实例保存到该registry对象
(3)注册所有公共类
set key to value
(4)创建Front类对象,作为请求分发器(Dispatcher),分发action
// Front Controller
$controller = new Front($registry);
// Maintenance Mode
$controller->addPreAction(new Action('common/maintenance'));
// SEO URL's
$controller->addPreAction(new Action('common/seo_url'));
// Router
if (isset($request->get['route'])) {
$action = new Action($request->get['route']);
} else {
$action = new Action('common/home');
}
// Dispatch
$controller->dispatch($action, new Action('error/not_found'));
(5)根据用户请求(url)创建控制器对象及其动作。
action类根据url得到action对应的类$class,然后执行类中的方法
在Front类私有函数execute($action)中如下语句
$controller = new $class($this->registry); //创建控制器
if (is_callable(array($controller, $this->method))) {
return call_user_func(array($controller, $this->method), $this->args);
} else {
return false;
}
(6)controller控制器里可以加载相应的模型,如
$this->load->model('design/layout');
该语句将创建相应的model对象。
(7)controller控制器里可以获取模板,绘制(提取数据并启用output buffer)到页面输出区output中
$this->load->view('payment/alipay_direct.tpl', $data)
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/account.tpl', $data));
(8)最后Response对象把输出区的数据(页面)echo返回给用户
public function output() {
if ($this->output) {
if ($this->level) {
$output = $this->compress($this->output, $this->level);
} else {
$output = $this->output;
}
if (!headers_sent()) {
foreach ($this->headers as $header) {
header($header, true);
}
}
echo $output;
}
}
###架构核心 核心在system/engine