thinkphp6 代码案例
以下是一个简单的 ThinkPHP 6 代码案例,用于创建一个简单的用户注册功能:
1. 创建数据库表格:
```sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
```
2. 创建控制器(app\controller\):
```php
<?php
namespace app\controller;
use think\Controller;
use think\Request;
use app\model\User;
class UserController extends Controller
{
public function index()
{
return $this->fetch();
}
常用的php代码实例 public function register(Request $request)
{
$data = $request->param();
$user = new User();
$user->username = $data['username'];
$user->password = password_hash($data['password'], PASSWORD_DEFAULT);
$user->email = $data['email'];
$user->save();
return json(['status' => 'success', 'message' => '注册成功']);
}
}
```
3. 创建模型(app\model\):
```php
<?php
namespace app\model;
use think\Model;
class User extends Model
{
}
```
4. 创建视图(app\view\user\):
```html
<!DOCTYPE html>
<html>
<head>
<title>用户注册</title>
</head>
<body>
<form action="/user/register" method="post">
<label for="username">用户名:</label>
<input type="text" name="username" id="username" required>
<br>
<label for="password">密码:</label>
<input type="password" name="password" id="password" required>
<br>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" required>
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
```
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论