跟着《PHP高性能开发:基础、框架与项目实战》_王甲临.pdf 学PHP,前5章没啥用,从第6章composer开始才有点用, 跟着第8章做下thinkphp的简单使用,后面几章全是项目 。。。
thinkphp6.0 学习笔记
文档:https://www.kancloud.cn/manual/thinkphp6_0/1037479
用PHPSTUDY, 安装好后运行NGINX
composer安装时有选择PHP的路径,选择phpstudy的那个路径,装好后命令行运行
composer --version
看看安装成功没
pupstudy_pro/www目录下命令行执行
composer create-project topthink/think tp5api
创建thinkphp项目,目录名叫tp5api, 2020年10月13日 下载的是thinkphp6的
phpstudy运行后浏览器看
http://localhost/tp5api/public
出现静态页,成功
安装build命令
composer require 'topthink/think-multi-app'
安装过程很慢很慢,上面的命令如果后面没有'',那会经常安装不了的,我自己测试加上''能下载,已经用了那个composer中国全量镜像了还是一样慢,不行就ctrl+c 结束了,再重新再来,有时cmd卡着不会自动刷新了,在cmd里按f5可刷新显示
装完后命令
php think
可看到build出来了
php think build api
创建api子应用,浏览器浏览
http://localhost/tp5api/public/index.php/api
可看到创建的东西
php think make:controller api@Order
在api应用下创建Order控制器, 打开后在index方法里返回字符串 return '您好!这是一个[api]/order控制器';
浏览器看
http://localhost/tp5api/public/index.php/api/order/index
可看到新建立的东西
php think make:model api@Order
生成api应用下的模型
做个示例:生成短网址的简单页面, 新闻页显示数据库中的内容,下面显示短网址,点短网址又跳回新闻页
phpstudy中建立网站,域名tp5api.com,指向c:/phpstudy_pro/www/tp5api/public, 建立后发现HOST文件也自动加上了,蛮好的
浏览器里输入
http://tp5api.com
可看到之前的页面效果
mysql里建立数据库tp5api,里面有表news(id,title,content)和shorturl(id,shorturl,url)
tp5api/config/database.php里修改好数据库名字和密码
tp5api/app/controller目录里复制Index.php为News.php,
也可以用命令php think make:controller News 创建
代码如下,根据传进来的ID取数据库数据,再赋值,返回页面
declare (strict_types = 1);
namespace app\controller;
use think\Request;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
class News extends BaseController
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
$id = input('id');
if(!$id){
$this->error('传入参数错误');
}
$map = [];
$map['id']=$id;
$info = Db::name('news')->where($map)->find();
if(!$info){
$this->error('查询数据为空');
}
View::assign('info',$info);
View::assign('shorturl',shorturl($this->request->url(true)));
return View::fetch();
}
ThinkPHP6.0里的页面模板需要安装以下东西:
composer require 'topthink/think-viewcomposer'
安装好后浏览器里访问
http://tp5api.com/news/index?id=1
可以看到效果
模板视图文件内容在 tp5api/app/view/news/index.html