## 开始使用
1. 填写好控制器名称
2. 填写好表单元素
3. 点同步字段,会将表单元素自动同步到表字段,如果不想建表,也要点一下同步,不然前端校验通过不了,包含 `id`、`status`、`isdelete`、`create_time`、`update_time` 的数据类型随便填写,模型会根据选项自动生成,不按此处选择的数据类型生成
4. 勾选需要生成的首页菜单
5. 勾选是否创建模型和验证器
6. 点击生成
![](https://box.kancloud.cn/b958c0ef4d453ce575f52a29b9e38d5a_1851x966.png)
![](https://box.kancloud.cn/ceb7a46fca5304ebb2e8dd3c88669e3d_1842x961.png)
![](https://box.kancloud.cn/7f943e99240e7d86ba656a2ebc43ff91_1836x965.png)
![](https://box.kancloud.cn/5ed815d6a9be67d13412594a3198a20c_1852x966.png)
![](https://box.kancloud.cn/8d273bc1da48f400e7a153a849259cf6_1853x965.png)
>[info] 以下代码、日志全部是自动生成,没有做任何修改,包括缩进,严格的缩进非常方便二次编辑和查阅
## 生成文件
### controller/one/two/ThreeFour.php
```
<?php
namespace app\admin\controller\one\two;
\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);
use app\admin\Controller;
class ThreeFour extends Controller
{
use \app\admin\traits\controller\Controller;
// 方法黑名单
protected static $blacklist = [];
protected static $isdelete = false;
protected function filter(&$map)
{
if ($this->request->param("text")) {
$map['text'] = ["like", "%" . $this->request->param("text") . "%"];
}
if ($this->request->param("textarea")) {
$map['textarea'] = ["like", "%" . $this->request->param("textarea") . "%"];
}
}
}
```
### view/one/two/three_four/index.html
```
{extend name="template/base" /}
{block name="content"}
<div class="page-container">
<form class="mb-20" method="get" action="{:\\think\\Url::build($Request.action)}">
<input type="text" class="input-text" style="width:250px" placeholder="文本" name="text" value="{$Request.param.text}" >
<div class="select-box" style="width:250px">
<select name="select" class="select">
<option value="key1">val1</option>
<option value="key2">val2</option>
</select>
</div>
<div class="select-box" style="width:250px">
<select name="radio" class="select">
<option value="">所有单选框</option>
{foreach name="$Think.config.conf.sex" item='v' key='k'}
<option value="{$k}">{$v}</option>
{/foreach}
</select>
</div>
<input type="text" class="input-text" style="width:250px" placeholder="文本域" name="textarea" value="{$Request.param.textarea}" >
<input type="text" class="input-text Wdate" style="width:250px" placeholder="日期" name="date" value="{$Request.param.date}" {literal} onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" {/literal} >
<button type="submit" class="btn btn-success"><i class="Hui-iconfont"></i> 搜索</button>
</form>
<div class="cl pd-5 bg-1 bk-gray">
<span class="l">
{tp:menu menu="add,forbid,resume" /}
</span>
<span class="r pt-5 pr-5">
共有数据 :<strong>{$count ?? '0'}</strong> 条
</span>
</div>
<table class="table table-border table-bordered table-hover table-bg mt-20">
<thead>
<tr class="text-c">
<th width="25"><input type="checkbox"></th>
<th width="">{:sort_by('文本','text')}</th>
<th width="">{:sort_by('下拉框','select')}</th>
<th width="">{:sort_by('单选框','radio')}</th>
<th width="">复选框</th>
<th width="">文本域</th>
<th width="">密码</th>
<th width="">{:sort_by('日期','date')}</th>
<th width="70">操作</th>
</tr>
</thead>
<tbody>
{volist name="list" id="vo"}
<tr class="text-c">
<td><input type="checkbox" name="id[]" value="{$vo.id}"></td>
<td>{$vo.text|high_light=$Request.param.text}</td>
<td>{$vo.select}</td>
<td>{$vo.radio}</td>
<td>{$vo.checkbox}</td>
<td>{$vo.textarea|high_light=$Request.param.textarea}</td>
<td>{$vo.password}</td>
<td>{$vo.date}</td>
<td class="f-14">
{$vo.status|show_status=$vo.id}
{tp:menu menu='sedit' /}
{tp:menu menu='sdeleteforever' /}
</td>
</tr>
{/volist}
</tbody>
</table>
<div class="page-bootstrap">{$page ?? ''}</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript" src="__LIB__/My97DatePicker/WdatePicker.js"></script>
<script>
$(function () {
$("[name='select']").find("[value='{$Request.param.select}']").attr("selected", true);
$("[name='radio']").find("[value='{$Request.param.radio}']").attr("selected", true);
})
</script>
{/block}
```
>[info] 因为没有选择创建回收站(recyclebin.html)自动合并 th.html、td.html、form.html 到 index.html 文件中
### view/one/two/three_four/edit.html
```
{extend name="template/base" /}
{block name="content"}
<div class="page-container">
<form class="form form-horizontal" id="form" method="post" action="{:\\think\\Request::instance()->baseUrl()}">
<input type="hidden" name="id" value="{:isset($vo.id)?$vo.id:''}">
<div class="row cl">
<label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>文本:</label>
<div class="formControls col-xs-6 col-sm-6">
<input type="text" class="input-text" placeholder="文本" name="text" value="{$vo.text ?? 'default_value'}" >
</div>
<div class="col-xs-3 col-sm-3"></div>
</div>
<div class="row cl">
<label class="form-label col-xs-3 col-sm-3">下拉框:</label>
<div class="formControls col-xs-6 col-sm-6">
<div class="select-box">
<select name="select" class="select" datatype="n" nullmsg="请选择下拉框" errormsg="请填写数字" ignore="ignore">
<option value="key1">val1</option>
<option value="key2">val2</option>
</select>
</div>
</div>
<div class="col-xs-3 col-sm-3"></div>
</div>
<div class="row cl">
<label class="form-label col-xs-3 col-sm-3">单选框:</label>
<div class="formControls col-xs-6 col-sm-6 skin-minimal">
{foreach name="$Think.config.conf.sex" item='v' key='k'}
<div class="radio-box">
<input type="radio" name="radio" id="radio-{$k}" value="{$k}">
<label for="radio-{$k}">{$v}</label>
</div>
{/foreach}
</div>
<div class="col-xs-3 col-sm-3"></div>
</div>
<div class="row cl">
<label class="form-label col-xs-3 col-sm-3">复选框:</label>
<div class="formControls col-xs-6 col-sm-6 skin-minimal">
<div class="radio-box">
<input type="checkbox" name="checkbox[]" id="checkbox-key1" value="key1">
<label for="checkbox-key1">val1</label>
</div>
<div class="radio-box">
<input type="checkbox" name="checkbox[]" id="checkbox-key2" value="key2">
<label for="checkbox-key2">val2</label>
</div>
</div>
<div class="col-xs-3 col-sm-3"></div>
</div>
<div class="row cl">
<label class="form-label col-xs-3 col-sm-3">文本域:</label>
<div class="formControls col-xs-6 col-sm-6">
<textarea class="textarea" placeholder="" name="textarea" onKeyUp="textarealength(this, 100)">{$vo.textarea ?? '我是文本域'}</textarea>
<p class="textarea-numberbar"><em class="textarea-length">0</em>/100</p>
</div>
<div class="col-xs-3 col-sm-3"></div>
</div>
<div class="row cl">
<label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>密码:</label>
<div class="formControls col-xs-6 col-sm-6">
<input type="password" class="input-text" placeholder="密码" name="password" value="{$vo.password ?? ''}" >
</div>
<div class="col-xs-3 col-sm-3"></div>
</div>
<div class="row cl">
<label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>日期:</label>
<div class="formControls col-xs-6 col-sm-6">
<input type="text" class="input-text Wdate" placeholder="日期" name="date" value="{$vo.date ?? '2016-11-11'}" {literal} onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" {/literal} >
</div>
<div class="col-xs-3 col-sm-3"></div>
</div>
<div class="row cl">
<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
<button type="submit" class="btn btn-primary radius"> 提交 </button>
<button type="button" class="btn btn-default radius ml-20" onClick="layer_close();"> 取消 </button>
</div>
</div>
</form>
</div>
{/block}
{block name="script"}
<script type="text/javascript" src="__LIB__/Validform/5.3.2/Validform.min.js"></script>
<script type="text/javascript" src="__LIB__/My97DatePicker/WdatePicker.js"></script>
<script>
$(function () {
$("[name='radio'][value='{$vo.radio ?? '0'}']").attr("checked", true);
var checks = ''.split(",");
if (checks.length > 0){
for (var i in checks){
$("[name='checkbox[]'][value='"+checks[i]+"']").attr("checked", true);
}
}
$("[name='select']").find("[value='{$vo.select ?? 'key1'}']").attr("selected", true);
$('.skin-minimal input').iCheck({
checkboxClass: 'icheckbox-blue',
radioClass: 'iradio-blue',
increaseArea: '20%'
});
$("#form").Validform({
tiptype: 2,
ajaxPost: true,
showAllError: true,
callback: function (ret){
ajax_progress(ret);
}
});
})
</script>
{/block}
```
### view/one/two/three_four/config.php
```
<?php
return array (
'module' => 'admin',
'menu' =>
array (
0 => 'add',
1 => 'forbid',
2 => 'resume',
),
'create_config' => true,
'controller' => 'one.two.ThreeFour',
'title' => '四级控制器',
'form' =>
array (
0 =>
array (
'title' => '文本',
'name' => 'text',
'type' => 'text',
'option' => '',
'default' => 'default_value',
'sort' => '1',
'search' => '1',
'search_type' => 'text',
'require' => '1',
'validate' =>
array (
'datatype' => '*',
'nullmsg' => '',
'errormsg' => '',
),
),
1 =>
array (
'title' => '下拉框',
'name' => 'select',
'type' => 'select',
'option' => 'key1:val1#key2:val2',
'default' => 'key1',
'sort' => '1',
'search' => '1',
'search_type' => 'select',
'validate' =>
array (
'datatype' => 'n',
'nullmsg' => '',
'errormsg' => '请填写数字',
),
),
2 =>
array (
'title' => '单选框',
'name' => 'radio',
'type' => 'radio',
'option' => '{sex}',
'default' => '0',
'sort' => '1',
'search' => '1',
'search_type' => 'select',
'validate' =>
array (
'datatype' => '',
'nullmsg' => '',
'errormsg' => '',
),
),
3 =>
array (
'title' => '复选框',
'name' => 'checkbox',
'type' => 'checkbox',
'option' => 'key1:val1#key2:val2',
'default' => '',
'search_type' => 'text',
'validate' =>
array (
'datatype' => '',
'nullmsg' => '',
'errormsg' => '',
),
),
4 =>
array (
'title' => '文本域',
'name' => 'textarea',
'type' => 'textarea',
'option' => '',
'default' => '我是文本域',
'search' => '1',
'search_type' => 'text',
'validate' =>
array (
'datatype' => '',
'nullmsg' => '',
'errormsg' => '',
),
),
5 =>
array (
'title' => '密码',
'name' => 'password',
'type' => 'password',
'option' => '',
'default' => '',
'search_type' => 'text',
'require' => '1',
'validate' =>
array (
'datatype' => '/^\\w{6,32}$/',
'nullmsg' => '请填写密码',
'errormsg' => '密码必须为6~32位',
),
),
6 =>
array (
'title' => '日期',
'name' => 'date',
'type' => 'date',
'option' => '',
'default' => '2016-11-11',
'sort' => '1',
'search' => '1',
'search_type' => 'date',
'require' => '1',
'validate' =>
array (
'datatype' => '/^\\d{4}(\\-\\d{2}){2}$/',
'nullmsg' => '',
'errormsg' => '日期必须是yyyy-mm-dd格式',
),
),
),
'create_table' => '1',
'table_engine' => 'InnoDB',
'table_name' => '',
'field' =>
array (
1 =>
array (
'name' => 'text',
'type' => 'varchar(255)',
'default' => '',
'not_null' => '1',
'comment' => '文本',
'extra' => '',
),
2 =>
array (
'name' => 'select',
'type' => 'tinyint(3)',
'default' => '1',
'not_null' => '1',
'key' => '1',
'comment' => '下拉框',
'extra' => 'unsigned',
),
3 =>
array (
'name' => 'radio',
'type' => 'tinyint(1)',
'default' => '0',
'not_null' => '1',
'key' => '1',
'comment' => '单选框',
'extra' => '',
),
4 =>
array (
'name' => 'checkbox',
'type' => 'varchar(255)',
'default' => '',
'not_null' => '1',
'comment' => '复选框',
'extra' => '',
),
5 =>
array (
'name' => 'textarea',
'type' => 'text',
'default' => 'NULL',
'not_null' => '1',
'comment' => '文本域',
'extra' => '',
),
6 =>
array (
'name' => 'password',
'type' => 'char(32)',
'default' => 'NULL',
'comment' => '密码',
'extra' => '',
),
7 =>
array (
'name' => 'date',
'type' => 'date',
'default' => 'NULL',
'comment' => '日期',
'extra' => '',
),
),
'model' => '1',
);
```
### model/one/two/ThreeFour.php
```
<?php
namespace app\admin\model\one\two;
use think\Model;
class ThreeFour extends Model
{
// 指定表名,不含前缀
protected $name = 'one_two_three_four';
}
```
## 生成数据表
下面是 Linux 版 Navicat 截图
![](https://box.kancloud.cn/7b154887a475a9b1f3457e0f338cfa51_582x326.png)
下面是从 Linux 版 Navicat 中拷贝出的建表语句
```
CREATE TABLE `tp_one_two_three_four` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '四级控制器主键',
`text` varchar(255) NOT NULL DEFAULT '' COMMENT '文本',
`select` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '下拉框',
`radio` tinyint(1) NOT NULL DEFAULT '0' COMMENT '单选框',
`checkbox` varchar(255) NOT NULL DEFAULT '' COMMENT '复选框',
`textarea` text NOT NULL COMMENT '文本域',
`password` char(32) DEFAULT NULL COMMENT '密码',
`date` date DEFAULT NULL COMMENT '日期',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态,1-正常 | 0-禁用',
PRIMARY KEY (`id`),
KEY `select` (`select`),
KEY `radio` (`radio`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='四级控制器';
```
## 生成日志
`runtime/log` 目录下的日志原文:
>[info] 在当天的日志中搜索 build_sql 即可搜索到,日志原文中的标记是 BUILD_SQL
```
[ 2016-11-10T20:43:25+08:00 ] 127.0.0.1 127.0.0.1 POST /admin/generate/run.html
[ log ] tpadmin.dev/admin/generate/run.html [运行时间:0.037622s][吞吐率:26.58req/s] [内存消耗:6,304.23kb] [文件加载:63]
[ sql ] BUILD_SQL:
DROP TABLE IF EXISTS `tp_one_two_three_four`;
CREATE TABLE `tp_one_two_three_four` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '四级控制器主键',
`text` varchar(255) NOT NULL DEFAULT '' COMMENT '文本',
`select` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '下拉框',
`radio` tinyint(1) NOT NULL DEFAULT '0' COMMENT '单选框',
`checkbox` varchar(255) NOT NULL DEFAULT '' COMMENT '复选框',
`textarea` text NOT NULL COMMENT '文本域',
`password` char(32) COMMENT '密码',
`date` date COMMENT '日期',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态,1-正常 | 0-禁用',
PRIMARY KEY (`id`),
KEY `select` (`select`),
KEY `radio` (`radio`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '四级控制器';
---------------------------------------------------------------
```
- 概要
- 开始使用
- 写在前面
- 目录结构
- 模板主题支持
- 前置方法before支持
- 数据返回
- 异常接管
- Rbac 权限管理
- 开始使用
- 用户管理
- 分组管理
- 节点管理
- 角色管理
- 使用流程
- 其他
- 代码自动生成 v1.2
- 示例一 - 一级控制器
- 示例二 - 多级控制器
- 示例三 - 从数据表生成
- 示例四 - 指定生成的文件
- 示例五 - 命令行模式
- 示例六 - 模拟命令行模式
- 代码自动生成
- 示例一 - 一级控制器
- 示例二 - 多级控制器
- 控制器
- 公共控制器
- traits 多继承 Controller
- 公开不授权控制器
- 其他控制器
- 标签扩展
- 模板
- 网站操作日志
- 节点图
- 行为驱动
- 其他后端方法
- Excel一键导出
- Excel一键导入
- 文件下载
- 邮件发送
- 七牛文件上传
- id加密
- 前端
- ajax请求
- 表单校验
- 丰富弹层
- 异步操作
- 表格溢出
- 随机字符串
- 自动面包屑导航
- 动态加载文件
- 文件上传
- Tab 切换
- 图片预览
- 二维码生成
- 日历组件
- 升级指导
- 更新日志
- FAQ