chrisoishi/slim-easy-template 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chrisoishi/slim-easy-template

Composer 安装命令:

composer require chrisoishi/slim-easy-template

包简介

Easy temaplate to start dev with slim

关键字:

README 文档

README

Easy template for SLIM V4 with pre configurations to start dev your API quickly.

The objective this template is use SLIM FRAMEWORK V4 with struct folders and basic helpers integrations of models, controller, database and auth (in future, maybe more).

This template is also way to implements API efficent, organized and scalable. You don't need many time to understand this template, if you already used SLIM.

You can use the Skeleton template also for develop with SLIM, this template is official: https://github.com/slimphp/Slim-Skeleton.

i used some ideias of skeleton template and laravel framework to make this.

Installation to test

  • Run:

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

  • Edit .env file with your database informations

  • Run migrations of auth system:

vendor\bin\phinx migrate

  • Run local server:

composer serve

Struct folders

- 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

Database

This template based in MySQL database and use Medoo to manage data.

More informations about Medoo -> https://medoo.in/

To configure you database, edit de .env file.

Migrations

For migrations, this template use phinx extension ( for docs https://phinx.org/)

The default folder migrations is in db\migrations

Models

This template, use classes of ApiSupport, avaiable in modules folder. This classes has develop by me =D (more informations about this classes will be at the end)

Basically you need create a file in models folder with the simple code:

<?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
}

The class Model extended, have many functions to manage data in database, i recommend to check this class file for learn the features of this class.

Controllers

The controllers also use the classes of ApiSupport, but are similar a examples in docs of 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;
	}
}

Validations request

To validations your request data, please check the example in AuthController. The validations use respect/validation package (for docs access https://github.com/Respect/Validation)

Middlewares

Has a example the middleware for authenticaton in this template. You also can check for more details about middlewares in SLIM docs.

Authentication

This template have a basic model authentication, but efficient. Have implemeted register,login, login with token and refresh token functions.

Are 3 files implemented:

  • AuthMiddleware class: to use in routes for enable authentication requirement
  • Auth class: with functions to register login login with token
  • AuthToken class:: model to manage the AuthTokens ( generate, refresh, expirations...)

You can edit as you want auth implements, just check the auth files.

To test authtentication, just check the routes file (configs/routes.php) to preview auth routes name.

Steps:

  • Login with email and password, and you received a token and refresh token
  • use the token in authorization head in your requests -> Bearer token
  • if you token is expired use the refresh token to get new token.

Register new user

Use the route "auth/register" with data:

  • email
  • name
  • password
  • password_confirm

Login

Use the route "auth/login" with data:

  • email
  • password

Adding AuthMiddleware in your requests

This is a example.

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

Check the routes files in "configs/routes.php"

Getting user logged in code

To get user logged after your request passed AuthMiddleware. use de Auth::$user code

Auth::$user #return the User classs model

ApiSupport

This modules contains my implementations helpers to manage Models, Controller, Auth, Middleware, many features still need to be done, but this classes is working.

If you need new functions in these classes, you can easily implement them, as they are not complex, they were made just to help.

In future, i will create a docs for this classes.

Tips

If you need manage Dates use Carbon, this packages has been added. https://carbon.nesbot.com/docs/

Thanks for read or use:)

chrisoishi/slim-easy-template 适用场景与选型建议

chrisoishi/slim-easy-template 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「template」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 chrisoishi/slim-easy-template 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chrisoishi/slim-easy-template 我们能提供哪些服务?
定制开发 / 二次开发

基于 chrisoishi/slim-easy-template 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-04