chrisoishi/slim-easy-template

使用 slim 开发的简单模板

v0.1.1 2020-12-04 19:36 UTC

This package is auto-updated.

Last update: 2024-09-05 03:28:19 UTC


README

SLIM V4 的简单模板,带有预配置,可快速开始 API 开发。

这个模板的目标是使用 SLIM 框架 V4,具有结构化的文件夹和模型、控制器、数据库和身份验证(未来可能更多)的基本辅助程序集成。

这个模板也是实现高效、有组织和可扩展 API 的方法。如果您已经使用了 SLIM,您不需要太多时间就能理解这个模板。

您还可以使用 Skeleton 模板来开发 SLIM,这个模板是官方的:https://github.com/slimphp/Slim-Skeleton

我在这个模板中使用了一些 Skeleton 模板和 Laravel 框架的想法。

安装测试

  • 运行

composer create-project chrisoishi/slim-easy-template slim_project_name

  • 编辑 .env 文件,添加您的数据库信息

  • 运行身份验证系统的迁移

vendor\bin\phinx migrate

  • 运行本地服务器

composer serve

结构化文件夹

- app
    - controllers 
        - all controllers here
    - helpers 
        - all functions helpers here
    - middlewares 
        - all middlewares here
	- models
	    -  all models database tables here
	- modules
	    - similiar packages of packgist, but simple and for use especifcs a your projects
- configs
- db
    - all migrations database here
- public

数据库

这个模板基于 MySQL 数据库,并使用 Medoo 来管理数据。

有关 Medoo 的更多信息 -> https://medoo.in/

要配置数据库,请编辑 .env 文件。

迁移

对于迁移,这个模板使用 phinx 扩展(有关文档 https://phinx.org/

默认迁移文件夹位于 db\migrations

模型

这个模板使用位于 modules 文件夹中的 ApiSupport 的类。这些类是由我开发的 =D(有关这些类的更多信息将在最后提供)

基本上,您需要在模型文件夹中创建一个包含简单代码的文件

<?php
namespace  App\Models;
use App\Modules\ApiSupport\Model;
class  User  extends  Model
{
	const table = "users"; #this name of table in database
	const pk = "id"; # this field name of primary key
	const  protected = ["password"]; # this array of fields name to not send in response
	const cast = ["id"=>"interger"] #this array to cast field to necessary type
}

扩展 Model 的类具有许多在数据库中管理数据的功能,我建议您查看这个类文件以了解这个类的功能。

控制器

控制器也使用 ApiSupport 的类,但类似于 SLIM 文档中的示例。

The
<?php
namespace App\Controllers;
use App\Modules\ApiSupport\Controller;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class HomeController extends Controller{
	public function home(Request $request, Response $response, array $args): Response{
		$response->getBody()->write("Hello world"));
		return $response;
	}
}

请求验证

为了验证您的请求数据,请查看 AuthController 中的示例。验证使用 respect/validation 包(有关文档请访问 https://github.com/Respect/Validation

中间件

这个模板有一个用于身份验证的中间件示例。您还可以在 SLIM 文档中查看有关中间件的更多详细信息。

身份验证

这个模板有一个基本但高效的身份验证模型,实现了注册、登录、使用令牌登录和刷新令牌的功能。

实现了 3 个文件

  • AuthMiddleware 类:用于路由中启用身份验证要求
  • Auth 类:包含注册、登录、使用令牌登录的功能
  • AuthToken 类:用于管理 AuthTokens(生成、刷新、过期等)

您可以根据需要编辑身份验证实现,只需查看身份验证文件。

要测试身份验证,只需查看 routes 文件(configs/routes.php)以预览身份验证路由名称。

步骤

  • 使用电子邮件和密码登录,您将收到一个令牌和刷新令牌
  • 在您的请求中在授权头中使用令牌 -> Bearer token
  • 如果您访问令牌已过期,请使用刷新令牌获取新令牌。

注册新用户

使用“auth/register”路由并携带数据

  • 电子邮件
  • 姓名
  • 密码
  • 密码确认

登录

使用“auth/login”路由并携带数据

  • 电子邮件
  • 密码

在请求中添加AuthMiddleware

这是一个示例。

$app->get('/', HomeController::class . ":home")->add(new AuthMiddleware());

检查“configs/routes.php”中的路由文件

获取用户登录代码

在请求通过AuthMiddleware后获取已登录用户,请使用Auth::$user代码

Auth::$user # 返回User类的模型

ApiSupport

此模块包含了我用于管理模型、控制器、认证、中间件等的实现助手,许多功能仍需完成,但这些类正在运行。

如果您需要这些类的新功能,可以轻松实现,因为它们并不复杂,只是为了帮助。

将来,我将为这些类创建文档。

提示

如果您需要管理日期,请使用Carbon,这个包已被添加。https://carbon.nesbot.com/docs/

感谢阅读或使用:)