一、安裝
1、下載安裝
從官網下載ThinkPHP5的壓縮包,解壓到本地。
thinkphp5/
├─application/
├─public/
│ ├─index.php
│ └─.htaccess
├─vendor/
├─.htaccess
├─composer.json
└─think
2、配置虛擬主機
配置虛擬主機,將根目錄指向thinkphp5/public/,在瀏覽器中輸入域名即可看到 ThinkPHP 的歡迎界面。
VirtualHost *:80
DocumentRoot "path/to/thinkphp5/public"
ServerName thinkphp5.dev
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
3、初始化
使用命令行,進入ThinkPHP5的根目錄,運行init命令完成初始化。
cd path/to/thinkphp5
php think init
二、路由
1、基礎路由
在路由配置文件route.php中,添加一個基礎路由。
Route::get('hello/:name', 'index/hello');
這樣就可以訪問/hello/{name}這個地址了。
例如訪問/hello/tp5,會映射到index控制器的hello方法,並將name參數值設置為tp5。
2、分組路由
可以定義多個路由分組,分別對應不同的固定前綴。比如:
Route::group('admin', function () {
Route::rule('user/add', 'admin/user/add');
Route::rule('user/edit/:id', 'admin/user/edit')->pattern(['id' => '\d+']);
Route::rule('user/delete/:id', 'admin/user/delete')->pattern(['id' => '\d+'])->method('DELETE');
});
3、RESTful路由
可以通過RESTful路由,直接映射到相應的方法,如get請求映射到index方法,post請求映射到save方法。
Route::resource('blog', 'blog')
這樣就可以訪問/blog的地址,並根據請求方法自動映射到相應的方法了。
三、控制器
1、基礎控制器
在application目錄下新建Index控制器,添加一個hello方法。
namespace app\index\controller;
class Index
{
public function hello($name = 'ThinkPHP5')
{
return 'hello,' . $name;
}
}
2、路由到控制器
在路由配置文件route.php中添加路由規則,將/hello/:name路由到Index控制器的hello方法。
Route::get('hello/:name', 'index/hello');
3、視圖
在application目錄下新建view/index/hello.html文件,編寫模板內容。
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<p>Hello,<?php echo $name; ?>!</p>
</body>
</html>
4、讀取視圖內容
在Index控制器的hello方法中,使用fetch方法讀取view/index/hello.html文件的內容,並將name參數值傳遞到模板。
namespace app\index\controller;
class Index
{
public function hello($name = 'ThinkPHP5')
{
return $this->fetch('index/hello', ['name' => $name]);
}
}
四、模型
1、定義模型
在application目錄下新建model/User.php文件,定義一個User模型類。
namespace app\index\model;
use think\Model;
class User extends Model
{
}
2、讀取數據
在Index控制器中,調用User模型的select方法,讀取所有User的數據。
namespace app\index\controller;
use app\index\model\User;
class Index
{
public function index()
{
$users = User::select();
return $this->fetch('index/index', ['users' => $users]);
}
}
3、顯示數據
在view/index/index.html文件中,使用foreach循環遍歷並顯示User的數據。
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>email</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $user): ?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['age']; ?></td>
<td><?php echo $user['email']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
五、資料庫操作
1、配置資料庫連接
在config/database.php文件中,配置資料庫連接信息。
return [
// 資料庫類型
'type' => 'mysql',
// 伺服器地址
'hostname' => 'localhost',
// 資料庫名
'database' => 'test',
// 用戶名
'username' => 'root',
// 密碼
'password' => '123456',
// 埠
'hostport' => '3306',
// 資料庫表前綴
'prefix' => 'think_',
];
2、增刪改查
在Index控制器中,調用User模型的方法,進行增刪改查操作。
namespace app\index\controller;
use app\index\model\User;
class Index
{
public function add()
{
$user = new User;
$user->name = 'John';
$user->age = 20;
$user->email = 'john@example.com';
$user->save();
return '添加用戶成功';
}
public function update($id)
{
$user = User::get($id);
$user->age = 25;
$user->save();
return '更新用戶成功';
}
public function delete($id)
{
$user = User::get($id);
$user->delete();
return '刪除用戶成功';
}
public function select()
{
$users = User::select();
return $this->fetch('index/index', ['users' => $users]);
}
}
六、總結
本文通過對ThinkPHP5菜鳥教程的詳細闡述,從安裝、路由、控制器、模型和資料庫操作等多個方面,為讀者提供了系統化的ThinkPHP5開發知識。希望本文能為讀者在ThinkPHP5的開發學習和實踐過程中提供幫助。
原創文章,作者:MTDYX,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/369015.html