系统的架构模式由系统的需求决定
通过入口文件设置调度方式
- 由于研发的系统大小不一, 方向不同, 选择一个合适的架构模式是有必要的
- 利用服务器路径重写功能, 如 .htaccess 来定制访问方式, 也可以什么都不做
- 按架构模式修改入口文件中 of::dispatch 的 类名 参数调用到指定入口类
- 入口类同样按架构模式调用其它层类
通过修改寻类方式深入定制
- 大部分情况, 我们并不需要通过这种方式来改变框架默认的寻类方式, 当然都有例外
- 通过 of::event('of::loadClass', {}) 接收加载的新类
- 通过 of::link 注册到 L 类中一些方法
-
也可通过
of::event('of::dispatch', {}) 与
设置框架配置预加载 配合封装架构模式
框架默认模式
创建入口 /index.php
<?php
//加载核心
require dirname(__FILE__) . '/include/of/of.php';
//调度代码, 这里的"c"参数假设是 "demo_test", "a"参数为 "index"
if (isset($_GET['c'])) {
//类名, 动作, 安全校验 只要加载类返回 true 就可以通过网络访问
$result = of::dispatch($_GET['c'], isset($_GET['a']) ? $_GET['a'] : 'index', true);
//返回数组转成json
if (is_array($result)) echo of_base_com_data::json($result);
}
创建调用文件 /demo/test.php
<?php
//控制层类名以 ctrl_ 开头
class demo_test extends L {
/**
* 描述 : 通过index.php?c=demo_test访问
*/
function index() {
调用成功
}
}
//返回与安全校验相同值时才能调用
return true;
设计一个MVC模式
创建入口 /index.php
<?php
//建立 M层: model, V层: view, C层: ctrl 对应文件夹
require dirname(__FILE__) . '/include/of/of.php';
//调度代码, 这里的"c"参数假设是 "demo_test", "a"参数为 "index"
if (isset($_GET['c'])) {
//直接调用 ctrl_demo_test 实例中的 index 方法, 直接访问 ctrl_ 层, 所以不需要安全校验 null
$result = of::dispatch('ctrl_' . $_GET['c'], isset($_GET['a']) ? $_GET['a'] : 'index', null);
//返回数组转成json
if (is_array($result)) echo json_encode($result);
}
创建控制层 /ctrl/demo/test.php
<?php
//控制层类名以 ctrl_ 开头
class ctrl_demo_test extends L {
/**
* 描述 : 通过index.php?c=demo_test访问
*/
function index() {
//调用model方法
model_demo_test::index();
}
}
设计一个HMVC模式
创建入口 /index.php
<?php
//建立一个 group 文件后在其内建立 M层: model, V层: view, C层: ctrl 对应文件夹
require dirname(__FILE__) . '/include/of/of.php';
//调度代码, 这里的"c"参数假设是 "group_demo_test", "a"参数为 "index", group 假定分组
if (isset($_GET['c'])) {
//提取组信息
$temp = explode('_', $_GET['c'], 2);
//直接调用 group_ctrl_demo_test 实例中的 index 方法, 直接访问 ctrl 层, 所以不需要安全校验 null
$result = of::dispatch(
$temp[0] . '\\ctrl\\' . strtr($temp[1], '_', '\\'),
isset($_GET['a']) ? $_GET['a'] : 'index',
null
);
//返回数组转成json
if (is_array($result)) echo json_encode($result);
}
创建控制层 /group/ctrl/demo/test.php
<?php
namespace group\ctrl\demo;
use group\model;
//注意在命名空间下使用框架要加"\"
class test extends \L {
/**
* 描述 : 通过index.php?c=group_demo_test访问
*/
function index() {
//调用model方法
model\demo\test::index();
}
}