创建module
php think build --module 模块名
eg:
php think build --module admin
创建controller
php think make:controller 模块名/控制器
eg:
php think make:controller admin/User
创建model
php think make:model 模块名/模型名
eg:
php think make:model admin/User
获取传入参数
$requet = request();
// 获取所有
$param = $request->param();
// 获取单个
$name = $request->param('name');
修改view
<?php
namespace app\admin\controller;
class Index
{
public function index()
{
return view(ROOT_PATH . '/public/admin/index.html');
}
}
查询数据
// ...
use \app\admin\model\User as UserModel;
// ...
$data = UserModel::select();
// 查询所有
foreach($data as $k=>$v)
{
$data[$k] = $v->toArray();
}
return json($data);
// 查询单条
$data = UserModel::get('name' => 'admin');
or
$data = UserModel::find('name' => 'admin');
or
$data = UserMode::where('id','1')->find();
增加数据
$data = UserModel::create([
'name' => 'admin'
]);
return $data->id;
更新数据
UserModel::where('name', 'admin')->update(['age' => '20']);
or
UserModel::update(['name' => 'admin', 'age' => '20']);
删除数据
UserModel::where('name', 'admin')->delete();
or
UserModel::destroy(1);
or
UserModel::destroy(['name' => 'admin']);
扩展助手函数文件helper
// 修改 application/config.php
// 扩展函数文件
'extra_file_list' => [
APP_PATH . 'helper' . EXT,
THINK_PATH . 'helper' . EXT
]
// 新建文件application/helper.php
<?php
function SayHello() {
return 'Hello';
}
使用验证器
use \think\Validate;
$validate = new Validate([
'username' => 'require|max:20',
'password' => 'require|max:20'
]);
$data = [
'username' => request()->param('username'),
'password' => request()->param('password')
];
if(!$validate->check($data)) {
return json(['error' => $validate->getError()]);
}
网友评论