企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 什么是thinkphp 1. 国人开发的php框架 2. 源码中文注释, 方便理解 3. 中文文档详尽 4. 使用者多, 教程丰富 ## 官网 * http://www.thinkphp.cn/ * https://www.kancloud.cn/manual/thinkphp5/118003(官方文档) ## 安装 1. 官网下载, 复制粘贴 2. 检查环境 3. 访问public文件夹 ## 配置虚拟主机 * 配置vhosts-conf * 修改hosts ## URL路径设计 * http://serverName/index.php(或者其它应用入口文件)/模块/控制器/操作/[参数名/参数值…] ## 格式化php代码 1. 安装phpfmt插件 ~~~ ctrl+shift+p 搜索phpfmt enter ~~~ 2. 配置插件 ~~~ { "format_on_save": true, "php_bin": "G:/phpStudy/php/php-7.0.12-nts/php.exe", } ~~~ 3. psr1和psr2区别 `花括号点位置不同` ## API友好 ~~~ <?php namespace app\index\controller; class Index { public function index() { $data = array( 'name' => 'red_panda', 'address' => 'China', ); $code = 200; $msg = 'ok'; return ['data' => $data, 'code' => $code, 'message' => $msg]; } } ~~~ ~~~ 'default_return_type'=>'json' ~~~ ## 获取请求参数 ~~~ <?php namespace app\index\controller; use \think\Request; class Index { public function index() { $request = Request::instance(); echo '请求方法:' . $request->method() . '<br/>'; echo '访问地址:' . $request->ip() . '<br/>'; echo '请求参数:'; dump($request->param()); echo '请求参数:仅包含name,sex'; dump($request->only(['name', 'sex'])); echo '请求参数:排除name,sex'; dump($request->except(['name', 'sex'])); } } ~~~ ## 判断请求类型 ~~~ // 是否为 GET 请求 if (Request::instance()->isGet()) echo "当前为 GET 请求"; // 是否为 POST 请求 if (Request::instance()->isPost()) echo "当前为 POST 请求"; // 是否为 PUT 请求 if (Request::instance()->isPut()) echo "当前为 PUT 请求"; // 是否为 DELETE 请求 if (Request::instance()->isDelete()) echo "当前为 DELETE 请求"; // 是否为 Patch 请求 if (Request::instance()->isPatch()) echo "当前为 PATCH 请求"; ~~~ ## 验证参数数据 ~~~ <?php namespace app\index\controller; use \think\Validate; class Index { public function index() { $rule = [ 'name' => 'require|max:25', 'age' => 'number|between:1,120', 'email' => 'email', ]; $msg = [ 'name.require' => '名称必须', 'name.max' => '名称最多不能超过25个字符', 'age.number' => '年龄必须是数字', 'age.between' => '年龄只能在1-120之间', 'email' => '邮箱格式错误', ]; $data = input('post.'); $validate = new Validate($rule, $msg); $result = $validate->check($data); if (!$validate->check($data)) { dump($validate->getError()); } } } ~~~ ## 连接数据库 ~~~ /* 数据库设置 */ 'database' => [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'thinkphp', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => '', // 数据库调试模式 'debug' => false, ], ~~~ ## 原生sql语句查询 ~~~ <?php namespace app\index\controller; use think\Db; class Index { public function index() { $res = Db::query('select version()'); return $res; } } ~~~