安裝 ThinkPHP6.0
cd 切換到項目目錄,然后執行如下
准備安裝框架:composer create-project topthink/think thinkPHP6.0
cd 切換到PHP項目 thinkPHP6.0 目錄中
修改配置文件:.env 配置文件 // 復制 .example.env 改成自己的
打開調試模式:.env 中的 APP_DEBUG = true
顯示錯誤消息:config/app.php 中的 show_error_msg = true
部署測試運行:php think run
可以指定端口:php think run -p 80
可以直接訪問:http://localhost:8000 // :后面是端口號
域名配置/URL重寫
根據自己的環境自行搭建,記得在 hosts 中增加
Apache
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
Nginx
location / { // …..省略部分代碼
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
控制器/模型/路由
資源控制器:php think make:controller Book
模型:php think make:model Book
資源路由:Route::resource('book', 'Book');
控制器
<?php
declare(strict_types=1);
namespace app\controller;
use app\BaseController;
use app\model\Book as ModelBook;
use think\Request;
class Book extends BaseController
{
/**
* 顯示資源列表
*
* @return \think\Response
*/
public function index()
{
$res = ModelBook::getPage();
return json(['code' => 0, 'msg' => 'ok', 'data' => $res]);
}
/**
* 顯示創建資源表單頁.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的資源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
// 數據接收
$data['title'] = $request->param('title', '未知');
$data['c_id'] = $request->param('c_id', '1');
$data['u_id'] = $request->param('u_id', '1');
$data['desc'] = $request->param('desc', '未知');
$data['img'] = $request->param('img', '未知');
// 數據驗證
// 執行添加
$res = ModelBook::addOne($data);
// 判斷
if (!$res) {
return json(['code' => 1, 'msg' => '添加劑失敗', 'data' => $res]);
}
return json(['code' => 0, 'msg' => 'ok', 'data' => $res]);
}
/**
* 顯示指定的資源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
$where['id'] = $id;
$res = ModelBook::getOne($where);
// 判斷
if (!$res) {
return json(['code' => 1, 'msg' => '獲取失敗', 'data' => $res]);
}
return json(['code' => 0, 'msg' => '獲取成功', 'data' => $res]);
}
/**
* 顯示編輯資源表單頁.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的資源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
// 修改條件
$where['id'] = $id;
// 接收數據
$data['title'] = $request->param('title', '修改');
$data['c_id'] = $request->param('c_id', '2');
$data['u_id'] = $request->param('u_id', '2');
$data['desc'] = $request->param('desc', '修改');
$data['img'] = $request->param('img', '修改');
// 執行
$res = ModelBook::upd($where, $data);
// 判斷
if (!$res) {
return json(['code' => 1, 'msg' => '修改失敗', 'data' => $res]);
}
return json(['code' => 0, 'msg' => '修改成功', 'data' => $res]);
}
/**
* 刪除指定資源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
$where['id'] = $id;
$res = ModelBook::del($where);
// 判斷
if (!$res) {
return json(['code' => 1, 'msg' => '刪除失敗', 'data' => $res]);
}
return json(['code' => 0, 'msg' => '刪除成功', 'data' => $res]);
}
}
模型
<?php
namespace app\model;
use think\Model;
class Book extends Model
{
protected $table = 'book';
protected $pk = 'id';
protected $schema = [
'id' => 'int',
'c_id' => 'int',
'u_id' => 'int',
'title' => 'string',
'desc' => 'string',
'img' => 'string',
'status' => 'int',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* 獲取一條數據
* @param array $where 條件
*/
public static function getOne($where)
{
return self::where($where)->find();
}
/**
* 分頁獲取數據
*/
public static function getPage()
{
return self::alias('b')->join('book_cate c', 'c.id = b.c_id')->field('b.*, c.name as cateName')->paginate(5);
}
/**
* 添加一條
* @param array $data 數據
*/
public static function addOne($data)
{
return self::create($data);
}
/**
* 刪除
* @param array $where 條件
*/
public static function del($where)
{
return self::where($where)->delete();
}
/**
* 修改
* @param array $where 條件
* @param array $data 數據
*/
public static function upd($where, $data)
{
return self::where($where)->save($data);
}
}
