//访问index.php,安全过滤、加载配置文件、核心启动文件、函数库、类库
//转载请注明: http://blog.csdn.net/dabao1989/article/details/21223585
//new注册表
$registry = new Registry(); //$registry里保存的config是根据当前店店铺(`oc_store`)获取`store_id`,然后到`oc_setting`里取出该店的配置信息,跟配置文件config.php无关
$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,最后几个类需要当前$registry作为参数,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')); //根据当前URL,加载指定Action,默认加载Action('common/home');
if (isset($request->get['route'])) {
$action = new Action($request->get['route']);
} else {
$action = new Action('common/home');
} //new Action()所进行的操作,根据URL路由,判断是否是一个控制器文件,如果是,break
//保存此控制器文件路径、当前需要执行的方法、参数等,假如方法名为空则要执行的方法名保存为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';//默认index
}
} //Front执行控制器, 先执行之前压入的前置控制器, 然后执行当前请求的控制器, 失败则执行控制器error/not_found
//前置控制器有seo_url,maintenance,后台应该可配置SEO,可能会根据SEO创建新的控制器并返回,覆盖,执行
//Front->dispatch()会调用$this->execute()执行控制器
//execute()内部通过调用is_callable()判断控制器方法是否可以调用,所以可以在子Controller中将index()定义为protected,防止直接调用
//当直接调用时child Controller中的protected方法时,将会转到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;//当不可用时,会调用$this->error $this->error = '';
}
} else {
$action = $this->error; $this->error = '';
} return $action;
} //假设执行的控制器是common/home, 没有指定方法, 则执行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()方法
//会判断children属性是否赋值,有的话会逐个new child Controller,child Controller仍可以$this->render(),但调用$this->render()必须设定$this->template
//$this->data数组存储的值可在模板中使用
//$this->data[basename[$child]]保存的是子模板,common/home.tpl可以通过调用echo $column_left、$column_right、$content_top、$content_bottom输出
//应注意$this->data数组中的键值不要和子模板名重复!
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中执行了$this->render(),父Controller在$this->data[basename($child)]才能捕获到子模板,否则捕获的是空字符串
//虽然child Controller中的index()被定义为protected, 但是可以被父Controller调用, 因为他们都继承自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数组
$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();//判断登录状态
$this->customer->logout();//退出操作
$this->customer->login();//登录操作
$this->getId();//获取用户ID,存储在customer表中的customer_id //Controller中的页面跳转,$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等静态文件加载,通过Document对象加载额外的JS,CSS链接,在common子模板中会自动输出
$registry->document;
$registry->document->addLink($href, $rel);
$registry->addStyle($href, $rel = 'stylesheet', $media = 'screen');
$registry->addScript($script); //币种 //模型机制
//在index.php创建数据库连接,并保存在$registry,根据DB_DRIVER不同,调用system/database/下指定的数据库引擎
//拥有4个方法:query(),escape(),countAffected(),getLastId()
//当验证外部数据时要用escape()进行安全过滤
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db); //数据表解析
//oc_setting 配置
//oc_url_alias 配合apache rewrite,进行商品及分类URL优化

opencart源码解析之 index.php的更多相关文章

  1. phpcms v9 源码解析-1 index.php

    这个是phpcms V9 的入口文件index.php. V9程序的执行绝大多数是从这个文件开始的,但不绝对,在项目下面的api.php和plugin.php是另外的入口文件,这里我们先不做深究. 在 ...

  2. 【转】opencart 源码解析

    前台控制程序列表-catalog/controller Catalog|controller|account 会员功能 |—— account.php 会员功能主頁|—— address.php 会员 ...

  3. Redux系列x:源码解析

    写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. ...

  4. jQuery2.x源码解析(设计篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) 这一篇笔者主要以设计的角度探索jQuery的源代 ...

  5. HashMap 源码解析

    HashMap简介: HashMap在日常的开发中应用的非常之广泛,它是基于Hash表,实现了Map接口,以键值对(key-value)形式进行数据存储,HashMap在数据结构上使用的是数组+链表. ...

  6. Surface在C++层的创建源码解析

    Surface在C++层的创建源码解析 源码为:android4.4.4 1.创建SurfaceComposerClient绘图客户端 // create a client to surfacefli ...

  7. 小学徒成长系列—StringBuilder & StringBuffer关键源码解析

    在前面的博文<小学徒成长系列—String关键源码解析>和<小学徒进阶系列—JVM对String的处理>中,我们讲到了关于String的常用方法以及JVM对字符串常量Strin ...

  8. Java集合---Array类源码解析

    Java集合---Array类源码解析              ---转自:牛奶.不加糖 一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Prim ...

  9. solr&lucene3.6.0源码解析(三)

    solr索引操作(包括新增 更新 删除 提交 合并等)相关UML图如下 从上面的类图我们可以发现,其中体现了工厂方法模式及责任链模式的运用 UpdateRequestProcessor相当于责任链模式 ...

随机推荐

  1. [Angular] Test Container component with async provider

    The main idea for testing contianer component is to make sure it setup everythings correctlly. Call ...

  2. Java算法--串的简单处理

    题目例如以下: 串的处理 在实际的开发工作中.对字符串的处理是最常见的编程任务. 本题目即是要求程序对用户输入的串进行处理.详细规则例如以下: 1. 把每个单词的首字母变为大写. 2. 把数字与字母之 ...

  3. php 模拟post的新发现,重点在最后的新方法

    最近两天项目需要,由于服务器正在开发,客户端进度稍快一些,没有服务器进行联调.因此我重操旧业,用PHP快速的写了一些web页面,算是当测试桩程序了,七八个web接口,基本上5到6个小时搞定了.由于当前 ...

  4. ubuntu系统安装和配置

    1.分区信息 1.1 /boot分区 这个分区包括了操作系统的内核和在启动系统过程中所要用到的文件.假设有了一个单独的/boot启动分区,即使基本的根分区出现了问题,计算机依旧可以启动.这个分区的大小 ...

  5. web项目中配置多个数据源

    web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源,  ...

  6. Web开发标配--开发人员工具-F12

    喜欢从业的专注,七分学习的态度. 360浏览器-开发工具 谷歌-开发工具 IE-开发工具 Web开发中最最烦琐的莫过于调整CSS和JS,而最方便最高效的方式就是利用浏览器的开发工具调整.CSS可以实时 ...

  7. 版本控制(1)——SVN

    一.工具下载 下载SVN: http://subversion.apache.org/ 我们选择Windows系统中的可视化的VisualSVN 如下图,左边是客户端,右边是服务器端,我们下载服务器端 ...

  8. Erlang 杂记

    学习Erlang的时候在书的留白处随手记录了一些东西,还有一些记录在了demo的注释里面,今天抽时间整理出来了一部分,分享一下. Erlang的设计哲学是为每一个独立的事件创建一个新进程. Erlan ...

  9. 学习鸟哥的Linux私房菜笔记(9)——bash1

    一.Shell简介 Shell :命令行解释器,是用户与系统沟通时的媒介 在Unix系统中有各种Shell, Linux采用bash为其默认shell 系统可以使用的shell记录在 /etc/she ...

  10. 详细阐述Web开发中的图片上传问题

    Web开发中,图片上传是一种极其常见的功能.但是呢,每次做上传,都花费了不少时间. 一个"小功能"花费我这么多时间,真心不愉快. So,要得认真分析下原因. 1.在最初学习Java ...