refactor: restructure API authentication system and remove legacy files
- Updated API routes to use a unified versioning scheme under `/api/v1/auth`. - Implemented new authentication controller for login, registration, and token management. - Removed legacy user and index controllers, along with associated models and validation files. - Updated documentation to reflect new API endpoints and usage. - Cleaned up unused service and middleware files to streamline the application structure.
This commit is contained in:
@@ -30,29 +30,32 @@ APP_DEBUG = true
|
||||
## 2. 路由和访问
|
||||
|
||||
### Q: 如何访问 API 接口?
|
||||
A: URL 格式为 `http://域名/应用名/控制器/方法`
|
||||
- 示例:`http://localhost:8000/api/index/index`
|
||||
- 或使用路由:`http://localhost:8000/api/index`(需配置路由)
|
||||
A: 当前项目使用固定路由入口,认证接口统一走 `http://域名/api/v1/auth/*`
|
||||
- 登录:`POST http://localhost:8000/api/v1/auth/login`
|
||||
- 注册:`POST http://localhost:8000/api/v1/auth/register`
|
||||
- 当前用户:`GET http://localhost:8000/api/v1/auth/me`
|
||||
|
||||
### Q: 如何添加新的路由?
|
||||
A: 在 `route/api.php` 文件中添加路由规则:
|
||||
A: 在 `app/api/route/app.php` 文件中添加路由规则:
|
||||
```php
|
||||
Route::get('user/profile', 'api.User/profile');
|
||||
Route::post('user/update', 'api.User/update');
|
||||
use app\api\controller\v1\Auth;
|
||||
|
||||
Route::post('v1/auth/login', [Auth::class, 'login']);
|
||||
Route::post('v1/auth/register', [Auth::class, 'register']);
|
||||
```
|
||||
|
||||
### Q: 如何设置路由参数?
|
||||
A: 使用 `:参数名` 格式:
|
||||
```php
|
||||
Route::get('user/:id', 'api.User/detail');
|
||||
Route::get('v1/example/:id', 'v1.Example/detail');
|
||||
```
|
||||
|
||||
## 3. 控制器开发
|
||||
|
||||
### Q: 如何创建新的控制器?
|
||||
A:
|
||||
1. 手动创建:在 `app/api/controller/` 目录下创建 PHP 文件
|
||||
2. 使用命令:`php think make:controller api@Demo`
|
||||
1. 手动创建:在 `app/api/controller/v1/` 目录下创建 PHP 文件
|
||||
2. 使用命令:`php think make:controller api@v1/Auth`
|
||||
|
||||
### Q: 如何返回 JSON 数据?
|
||||
A: 使用基础控制器提供的方法:
|
||||
@@ -97,7 +100,10 @@ $this->validate($data, [
|
||||
|
||||
2. 使用验证器:
|
||||
```php
|
||||
$this->validate($data, 'app\api\validate\User.register');
|
||||
$this->validate($data, [
|
||||
'username' => 'require|alphaNum',
|
||||
'password' => 'require|length:6,20',
|
||||
]);
|
||||
```
|
||||
|
||||
### Q: 如何自定义验证错误信息?
|
||||
@@ -137,8 +143,8 @@ A:
|
||||
1. 全局中间件:在 `app/api/middleware.php` 中注册
|
||||
2. 路由中间件:在路由定义中使用
|
||||
```php
|
||||
Route::group('api', function () {
|
||||
Route::get('user/info', 'api.User/info');
|
||||
Route::group('v1/auth', function () {
|
||||
Route::get('me', [\app\api\controller\v1\Auth::class, 'me']);
|
||||
})->middleware(\app\api\middleware\Auth::class);
|
||||
```
|
||||
|
||||
@@ -148,27 +154,28 @@ Route::group('api', function () {
|
||||
A:
|
||||
```php
|
||||
// 查询单条
|
||||
$user = \app\api\model\User::find(1);
|
||||
$member = \app\api\model\Member::findByUserid(1);
|
||||
|
||||
// 查询多条
|
||||
$users = \app\api\model\User::where('status', 1)->select();
|
||||
$members = \app\api\model\Member::where('disabled', 0)->select();
|
||||
|
||||
// 新增
|
||||
$user = new \app\api\model\User;
|
||||
$user->username = 'test';
|
||||
$user->save();
|
||||
$member = new \app\api\model\Member;
|
||||
$member->username = 'test';
|
||||
$member->password = password_hash('123456', PASSWORD_DEFAULT);
|
||||
$member->save();
|
||||
|
||||
// 或
|
||||
\app\api\model\User::create([
|
||||
\app\api\model\Member::create([
|
||||
'username' => 'test',
|
||||
'password' => '123456',
|
||||
'password' => password_hash('123456', PASSWORD_DEFAULT),
|
||||
]);
|
||||
|
||||
// 更新
|
||||
\app\api\model\User::update(['id' => 1, 'status' => 0]);
|
||||
\app\api\model\Member::update(['userid' => 1, 'disabled' => 1]);
|
||||
|
||||
// 删除
|
||||
\app\api\model\User::destroy(1);
|
||||
\app\api\model\Member::destroy(1);
|
||||
```
|
||||
|
||||
### Q: 如何使用事务?
|
||||
|
||||
Reference in New Issue
Block a user