thinkphp6 系统安装篇 --web开发简要流程系列
安装thinkphp框架
使用composer安装thinkphp6,composer会根据PHP的版本安装适合的版本,我的运行环境为小皮面板,PHP >= 7.1.0。
1 | composer create-project topthink/think tp |
说明:创建新项目,保存目录为tp,可以自定义。
开启多应用模式
1 | composer require topthink/think-multi-app |
开启多应用后,才可使用composer在应用目录使用build命令创建新应用
1 | php think build admin |
说明:上面的命令创建了admin应用,同理可以创建home应用,访问地址为:http://servername/应用名称/控制器/方法,需要配置伪静态,未配置的情况下为http://serverName/index.php/应用名称/控制器/方法。
thinkphp6需要自己安装视图模板引擎
1 | composer require topthink/think-view |
通常可以直接使用 think\facade\View 来操作视图。
安装系统内置的验证码
1 | composer require topthink/think-captcha |
配置伪静态规则,实现隐藏路径上的index.php
nginx配置伪静态,把下面的内容保存为nginx.htaccess文件放到应用入口public文件的同级目录下
1 2 3 4 5 6 | location / { if ( !-e $request_filename ){ rewrite ^/(.*)$ /index.php?s= $1 last; break ; } } |
apache伪静态规则 ,把下面的内容保存为.htaccess文件放到应用入口public文件的同级目录下(官方)
1 2 3 4 5 6 7 8 9 | <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews DirectoryIndex index.php RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [L,E=PATH_INFO: $1 ] </IfModule> |
apache伪静态规则未测试
thinkphp开启调试
通过 create-project 默认安装的话, 会在根目录自带一个 .example.env 文件,你可以直接重命名为 .env 文件。
APP_DEBUG = true
PHPStorm 对 ThinkPHP6.0 没有代码提示解决办法(方法来自互联网)
第一步,找到Model.php
文件路径 vendor\topthink\think-orm\src\Model.php
* @method Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件 * @method Query whereTime(string $field, string $op, mixed $range = null) static 查询日期和时间 * @method Query whereBetweenTime(string $field, mixed $startTime, mixed $endTime) static 查询日期或者时间范围 * @method Query whereBetweenTimeField(string $startField, string $endField) static 查询当前时间在两个时间字段范围 * @method Query whereYear(string $field, string $year = 'this year') static 查询某年 * @method Query whereMonth(string $field, string $month = 'this month') static 查询某月 * @method Query whereDay(string $field, string $day = 'today') static 查询某日 * @method Query whereRaw(string $where, array $bind = []) static 表达式查询 * @method Query whereExp(string $field, string $condition, array $bind = []) static 字段表达式查询 * @method Query when(mixed $condition, mixed $query, mixed $otherwise = null) static 条件查询 * @method Query join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询 * @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询 * @method Query with(mixed $with) static 关联预载入 * @method Query count(string $field) static Count统计查询 * @method Query min(string $field) static Min统计查询 * @method Query max(string $field) static Max统计查询 * @method Query sum(string $field) static SUM统计查询 * @method Query avg(string $field) static Avg统计查询 * @method Query field(mixed $field, boolean $except = false) static 指定查询字段 * @method Query fieldRaw(string $field, array $bind = []) static 指定查询字段 * @method Query union(mixed $union, boolean $all = false) static UNION查询 * @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT * @method Query order(mixed $field, string $order = null) static 查询ORDER * @method Query orderRaw(string $field, array $bind = []) static 查询ORDER * @method Query cache(mixed $key = null, integer $expire = null) static 设置查询缓存 * @method mixed value(string $field) static 获取某个字段的值 * @method array column(string $field, string $key = '') static 获取某个列的值 * @method Model find(mixed $data = null) static 查询单个记录 不存在返回Null * @method Model findOrEmpty(mixed $data = null) static 查询单个记录 不存在返回空模型 * @method \think\model\Collection select(mixed $data = null) static 查询多个记录 * @method Model withAttr(array $name, \Closure $closure) 动态定义获取器
第二步,找到DbManager.php
文件路径 vendor\topthink\think-orm\src\DbManager.php
* @method \think\db\Query master() static 从主服务器读取数据 * @method \think\db\Query readMaster(bool $all = false) static 后续从主服务器读取数据 * @method \think\db\Query table(string $table) static 指定数据表(含前缀) * @method \think\db\Query name(string $name) static 指定数据表(不含前缀) * @method \think\db\Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件 * @method \think\db\Query whereRaw(string $where, array $bind = []) static 表达式查询 * @method \think\db\Query whereExp(string $field, string $condition, array $bind = []) static 字段表达式查询 * @method \think\db\Query when(mixed $condition, mixed $query, mixed $otherwise = null) static 条件查询 * @method \think\db\Query join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询 * @method \think\db\Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询 * @method \think\db\Query field(mixed $field, boolean $except = false) static 指定查询字段 * @method \think\db\Query fieldRaw(string $field, array $bind = []) static 指定查询字段 * @method \think\db\Query union(mixed $union, boolean $all = false) static UNION查询 * @method \think\db\Query limit(mixed $offset, integer $length = null) static 查询LIMIT * @method \think\db\Query order(mixed $field, string $order = null) static 查询ORDER * @method \think\db\Query orderRaw(string $field, array $bind = []) static 查询ORDER * @method \think\db\Query cache(mixed $key = null , integer $expire = null) static 设置查询缓存 * @method \think\db\Query withAttr(string $name,callable $callback = null) static 使用获取器获取数据 * @method mixed value(string $field) static 获取某个字段的值 * @method array column(string $field, string $key = '') static 获取某个列的值 * @method mixed find(mixed $data = null) static 查询单个记录 * @method mixed select(mixed $data = null) static 查询多个记录 * @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录 * @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID * @method integer insertAll(array $dataSet) static 插入多条记录 * @method integer update(array $data) static 更新记录 * @method integer delete(mixed $data = null) static 删除记录 * @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据 * @method \Generator cursor(mixed $data = null) static 使用游标查找记录 * @method mixed query(string $sql, array $bind = [], boolean $master = false, bool $pdo = false) static SQL查询 * @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行 * @method \think\Paginator paginate(integer $listRows = 15, mixed $simple = null, array $config = []) static 分页查询 * @method mixed transaction(callable $callback) static 执行数据库事务 * @method void startTrans() static 启动事务 * @method void commit() static 用于非自动提交状态下面的查询提交 * @method void rollback() static 事务回滚 * @method boolean batchQuery(array $sqlArray) static 批处理执行SQL语句 * @method string getLastInsID(string $sequence = null) static 获取最近插入的ID
模板常量配置,引入静态资源路径
文件路径config\view.php 配置文件中进行定义
//定义模板目录 'tpl_replace_string' =>array( '{__STATIC__}'=>'/static' )
ThinkPHP 6 安装跳转提示扩展。
在应用根目录执行composer安装 liliuwei/thinkphp-jump
1 | composer require liliuwei/thinkphp-jump |
使用方法:
<?php namespace app\controller; class Index { // 在控制器中引入 Jump use \liliuwei\think\Jump; public function index(){ return $this->success('成功','跳转控制器'); return $this->error('失败'); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?