演示准备
- 按正式部署完成操作后, 根目录仅有include, index.php 和 license.txt
- 在index.php同级分别创建 model view ctrl 文件夹, 形成一个MVC架构模式
- 当然也可以使用其它模式
- 访问时, 控制层类名不是"serv"和"of"开头的, 默认会开启session, 详见 框架配置_of.session.autoStart 与 框架会话
Hello World 一个面向过程的小脚本
创建入口文件 /test.php 文件
<?php /** * 描述 : 入口文件, 支持面向过程开发 */ //框架唯一入口 require dirname(__FILE__) . '/include/of/of.php'; //调用文本封装生成唯一值 echo of_base_com_str::uniqid(), "<br>\n"; //调用快捷方法查询SQL语句 print_r(L::sql('SELECT 1'));
运行 /test.php 结果如下
fe6583ee08684d9587bac3b5b564f883 Array ( [0] => Array ( [1] => 1 ) )
常规演示 中小系统推荐使用
创建模型层 /model/test.php
<?php /** * 模型层, 类的命名方式 规范注意第4条 */ class model_test { /** * 描述 : 返回传递前端的参数 */ public static function argv() { return '后台参数传递到视图层'; } }
创建控制层 /ctrl/test.php 文件
<?php
/**
* 控制层
*/
class ctrl_test extends L { //L内提供了快速访问的方法,如数据库,语言包,组件等静态方法 继承后使用更方便
/**
* 描述 : 在入口文件中我们定义了默认请求方法为index
*/
public function index() { //通过index.php?c=ctrl_test访问
echo '这个默认的加载方法';
}
/**
* 描述 : 使用视图层
*/
public function view() { //通过index.php?c=ctrl_test&a=view访问
$this->view->argA = model_test::argv(); //向视图层传递数据,没继承L,可以操作 of_view::inst() 返回的对象
$this->view->argB = of_base_com_str::uniqid(); //生成唯一值
$this->view->argC = '<input>'; //属性不带下划线的会被编码
$this->view->_argD = '<input>'; //属性带下划线的不会被编码
$this->display('/test.html'); //加载视图模板,映射的方法是 of_view::display(模板路径)
}
}
return true; //安全校验,允许访问的文件返回值必须与控制层校验值相同
创建视图层 /view/test.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>使用模板引擎作为试图</title>
</head>
<body>
<input _value="$this->argA" value="编译前">
<input _value="$this->argB" value="唯一值">
<!--<?
echo $this->argC, $this->_argD;
?>-->
</body>
</html>
运行 /?c=ctrl_test&a=view 结果如下
<input>
命名空间 大型系统推荐使用
修改入口文件 /index.php
<?php
/**
* 描述 : 控制层共享文件, 控制层文件与类名相同, 以$_GET['a']作为方法名(默认index)
*/
//加载核心
require dirname(__FILE__) . '/include/of/of.php';
//调度代码
if (isset($_GET['c'])) {
//类名, 动作, 安全校验
$result = of::dispatch(
strtr($_GET['c'], '_', '\\'), //改成使用命名方式调用
isset($_GET['a']) ? $_GET['a'] : 'index',
PHP_SAPI === 'cli' ? null : true
);
//返回数组转成json
if (is_array($result)) echo \of\base\com\data::json($result); //依然可以是 of_base_com_data 方式调用
}
创建模型层 /model/test.php
<?php
namespace model;
/**
* 模型层
*/
class test {
/**
* 描述 : 返回传递前端的参数
*/
public static function argv() {
return '后台参数传递到视图层';
}
}
创建控制层 /ctrl/test.php 文件
<?php
namespace ctrl;
use of\base\com\str;
/**
* 控制层
*/
class test extends \L { //L是根命名空间, 故需要用"\"
/**
* 描述 : 定义的默认请求方法为index
*/
public function index() {
echo '这个默认的加载方法';
}
/**
* 描述 : 使用视图层
*/
public function view() {
$this->view->argA = \model\test::argv();
$this->view->argB = str::uniqid();
$this->display('/test.html');
}
}
return true;
创建视图层 /view/test.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>使用模板引擎作为试图</title>
</head>
<body>
<input _value="$this->argA" value="编译前">
<input _value="$this->argB" value="唯一值">
</body>
</html>