原文轉(zhuǎn)載:https://blog.csdn.net/dabao1989/article/details/21223585
//訪問index.php,安全過濾诉字、加載配置文件、核心啟動(dòng)文件、函數(shù)庫、類庫
//new注冊(cè)表
$registry = new Registry();
//$registry里保存的config是根據(jù)當(dāng)前店店鋪(`oc_store`)獲取`store_id`,然后到`oc_setting`里取出該店的配置信息墩朦,跟配置文件config.php無關(guān)
$query = $db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE store_id = '0' OR store_id = '" . (int)$config->get('config_store_id') . "' ORDER BY store_id ASC");
foreach ($query->rows as $setting) {
if (!$setting['serialized']) {
$config->set($setting['key'], $setting['value']);
} else {
$config->set($setting['key'], unserialize($setting['value']));
}
}
//依次new核心類寄纵,壓入$registry,最后幾個(gè)類需要當(dāng)前$registry作為參數(shù),new之后再重新壓入$registry
$registry->set('customer', new Customer($registry));
//new Front();加載前置Action
$controller = new Front($registry);
$controller->addPreAction(new Action('common/seo_url'));
$controller->addPreAction(new Action('common/maintenance'));
//根據(jù)當(dāng)前URL,加載指定Action,默認(rèn)加載Action('common/home');
if (isset($request->get['route'])) {
$action = new Action($request->get['route']);
} else {
$action = new Action('common/home');
}
//new Action()所進(jìn)行的操作,根據(jù)URL路由,判斷是否是一個(gè)控制器文件匿又,如果是,break
//保存此控制器文件路徑建蹄、當(dāng)前需要執(zhí)行的方法碌更、參數(shù)等,假如方法名為空則要執(zhí)行的方法名保存為index
function __construct($route, $args = array()) {
$path = '';
$parts = explode('/', str_replace('../', '', (string)$route));
foreach ($parts as $part) {
$path .= $part;
if (is_dir(DIR_APPLICATION . 'controller/' . $path)) {
$path .= '/';
array_shift($parts);
continue;
}
if (is_file(DIR_APPLICATION . 'controller/' . str_replace(array('../', '..\\', '..'), '', $path) . '.php')) {
$this->file = DIR_APPLICATION . 'controller/' . str_replace(array('../', '..\\', '..'), '', $path) . '.php';
$this->class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path);
array_shift($parts);
break;
}
}
if ($args) {
$this->args = $args;
}
$method = array_shift($parts);
if ($method) {
$this->method = $method;
} else {
$this->method = 'index';//默認(rèn)index
}
}
//Front執(zhí)行控制器, 先執(zhí)行之前壓入的前置控制器洞慎, 然后執(zhí)行當(dāng)前請(qǐng)求的控制器痛单, 失敗則執(zhí)行控制器error/not_found
//前置控制器有seo_url,maintenance,后臺(tái)應(yīng)該可配置SEO劲腿,可能會(huì)根據(jù)SEO創(chuàng)建新的控制器并返回旭绒,覆蓋,執(zhí)行
//Front->dispatch()會(huì)調(diào)用$this->execute()執(zhí)行控制器
//execute()內(nèi)部通過調(diào)用is_callable()判斷控制器方法是否可以調(diào)用焦人,所以可以在子Controller中將index()定義為protected挥吵,防止直接調(diào)用
//當(dāng)直接調(diào)用時(shí)child Controller中的protected方法時(shí),將會(huì)轉(zhuǎn)到error/not_found
$controller->dispatch($action, new Action('error/not_found'));
function dispatch($action, $error) {
$this->error = $error;
foreach ($this->pre_action as $pre_action) {
$result = $this->execute($pre_action);
if ($result) {
$action = $result;//重置
break;
}
}
while ($action) {
$action = $this->execute($action);
}
}
function execute($action) {
if (file_exists($action->getFile())) {
require_once($action->getFile());
$class = $action->getClass();
$controller = new $class($this->registry);
if (is_callable(array($controller, $action->getMethod()))) {
$action = call_user_func_array(array($controller, $action->getMethod()), $action->getArgs());
} else {
$action = $this->error;//當(dāng)不可用時(shí)花椭,會(huì)調(diào)用$this->error
$this->error = '';
}
} else {
$action = $this->error;
$this->error = '';
}
return $action;
}
//假設(shè)執(zhí)行的控制器是common/home, 沒有指定方法忽匈, 則執(zhí)行index()
class ControllerCommonHome extends Controller {
public function index() {
$this->document->setTitle($this->config->get('config_title'));
$this->document->setDescription($this->config->get('config_meta_description'));
$this->data['heading_title'] = $this->config->get('config_title');
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
} else {
$this->template = 'default/template/common/home.tpl';
}
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
$this->response->setOutput($this->render());
}
}
//Controller中的render()方法
//會(huì)判斷children屬性是否賦值,有的話會(huì)逐個(gè)new child Controller,child Controller仍可以$this->render(),但調(diào)用$this->render()必須設(shè)定$this->template
//$this->data數(shù)組存儲(chǔ)的值可在模板中使用
//$this->data[basename[$child]]保存的是子模板矿辽,common/home.tpl可以通過調(diào)用echo $column_left丹允、$column_right郭厌、$content_top、$content_bottom輸出
//應(yīng)注意$this->data數(shù)組中的鍵值不要和子模板名重復(fù)雕蔽!
function render() {
foreach ($this->children as $child) {
$this->data[basename($child)] = $this->getChild($child);
}
if (file_exists(DIR_TEMPLATE . $this->template)) {
extract($this->data);//模板中使用
ob_start();
require(DIR_TEMPLATE . $this->template);
$this->output = ob_get_contents();
ob_end_clean();
return $this->output;
} else {
trigger_error('Error: Could not load template ' . DIR_TEMPLATE . $this->template . '!');
exit();
}
}
//controller中的getChild()方法
//只有在child Controller中執(zhí)行了$this->render(),父Controller在$this->data[basename($child)]才能捕獲到子模板折柠,否則捕獲的是空字符串
//雖然child Controller中的index()被定義為protected, 但是可以被父Controller調(diào)用, 因?yàn)樗麄兌祭^承自Controller
function getChild($child, $args = array()) {
$action = new Action($child, $args);
if (file_exists($action->getFile())) {
require_once($action->getFile());
$class = $action->getClass();
$controller = new $class($this->registry);
$controller->{$action->getMethod()}($action->getArgs());
return $controller->output;
} else {
trigger_error('Error: Could not load controller ' . $child . '!');
exit();
}
}
//在主Controlle中,this->render()返回的模板需要傳送給$this->response->setOutput()才能輸出
$this->response->setOutput($this->render());
//Controller中的語言包使用,$this->language保存在$registry中
$this->language->load('common/footer');
$this->data['text_information'] = $this->language->get('text_information');
$this->data['text_service'] = $this->language->get('text_service');
//Controller中session使用,$this->session保存在$registry中
$this->session->data[$key];
$this->session->data[$key] = 'hello';
//Controller中的$this->request數(shù)組
$this->get = $_GET;
$this->post = $_POST;
$this->request = $_REQUEST;
$this->cookie = $_COOKIE;
$this->files = $_FILES;
$this->server = $_SERVER;
$this->request->get['route'];//獲取方式
//Controller中的$this->response使用
$this->response->setOutput($this->render());
$this->response->setOutput(json_encode($json));
$this->response->addHeader('Content-Type: application/xml');
//Controller中的$this->customer
$this->customer->isLogged();//判斷登錄狀態(tài)
$this->customer->logout();//退出操作
$this->customer->login();//登錄操作
$this->getId();//獲取用戶ID,存儲(chǔ)在customer表中的customer_id
//Controller中的頁面跳轉(zhuǎn),$url->link()用于生成控制器的鏈接,如下
$this->redirect($this->url->link('catalog/information', 'token=' . $this->session->data['token'] . $url, 'SSL'));
//Controller中的forward(),用于new 新的控制器萎羔,并返回
function forward($route, $args = array()) {
return new Action($route, $args);
}
//Controller中的模型加載
$this->load->model('catalog/category');
$this->model_catalog_category;//使用格式
//cache使用(原始使用文件緩存)
$this->cache->get($key);
$this->cache->set($key, $value);
$this->cache->delete($key);
//js,css等靜態(tài)文件加載,通過Document對(duì)象加載額外的JS,CSS鏈接,在common子模板中會(huì)自動(dòng)輸出
$registry->document;
$registry->document->addLink($href, $rel);
$registry->addStyle($href, $rel = 'stylesheet', $media = 'screen');
$registry->addScript($script);
//幣種
//模型機(jī)制
//在index.php創(chuàng)建數(shù)據(jù)庫連接,并保存在$registry,根據(jù)DB_DRIVER不同液走,調(diào)用system/database/下指定的數(shù)據(jù)庫引擎
//擁有4個(gè)方法:query(),escape(),countAffected(),getLastId()
//當(dāng)驗(yàn)證外部數(shù)據(jù)時(shí)要用escape()進(jìn)行安全過濾
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);
//數(shù)據(jù)表解析
//oc_setting 配置
//oc_url_alias 配合apache rewrite,進(jìn)行商品及分類URL優(yōu)化