• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel Application skeleton for me.

Laravel API 基础模板

开箱即用的 Laravel API 基础结构。

自己用的哈,仅供参考,不提供咨询解答服务。

特点

  • 内置 laravel/sanctum 的授权机制;
  • 高度完善的控制器、模型、模块模板;
  • 集成常用基础扩展;
  • 内置模型通用高阶 Traits 封装;
  • 自动注册 Policies;
  • 内置用户系统和基础接口;
  • 内置管理后台接口;

安装

  1. 创建项目
$ composer create-project overtrue/laravel-skeleton -vvv
  1. 创建配置文件
$ cp .env.example .env
  1. 创建数据表和测试数据
$ php artisan migrate --seed

这一步将会创建管理员账号 username:admin / password:changeThis!! 和一个 demo 设置项。

然后访问 http://laravel-skeleton.test/api/settings 将会看到演示的设置项信息。

使用

创建新模块

$ php artisan make:model Post -a --api
# Model created successfully.
# Factory created successfully.
# Created Migration: 2020_09_22_150134_create_posts_table
# Seeder created successfully.
# Controller created successfully.

模型关系加载

我们可以在前端请求时动态决定接口返回哪些模型关系,例如 User 模型有一个 posts 关系,我们的用户列表控制器如下 UserController@index:

public function index(Request $request)
{
    $users = User::filter($request->all())
        ->with($request->relations())   // <---
        ->latest()
        ->paginate($request->get('per_page', 20));

    return $users;
}

默认不会返回 posts 关系,当前端请求的 URL 如下时将会返回:

http://laravel-skeleton.test/api/users?with=posts

如果期望返回指定的字段,则可以这样构造 URL:

http://laravel-skeleton.test/api/users?with=posts:id,title,updated_at

这样返回结果中 posts 结果将只包含 id,title,updated_at

注意:这里不能省略掉关系的 id 字段,否则关系将无法正常加载。

模型搜索功能

项目已经内置的基本的搜索支持,您只需要在模型引入 App\Traits\Filterable,然后配置 filterable属性即可:

use use App\Traits\Filterable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Filterable;

    protected $filterable = [
        'user_id', 'category_id', 'version', 
    ];
}

控制器开启搜索功能,只需要调用 filter() 方法即可:

public function index(Request $request)
{
    $posts = Post::with($request->relations())
                ->latest()
                ->filter() // <---
                ->paginate($request->get('per_page'));

    return $posts;
}

URL 中只需要传递对应的参数值即可:

http://laravel-skeleton.test/api/posts?with=user:id,username&user_id=123&category_id=4
// &user_id=123&category_id=4

自定义搜索

默认使用相等查询,如果需要自定义搜索字段,直接在模型中添加 filterXXX 方法实现,比如我们想实现文章标题模糊查询:

public function filterTitle($query, $keyword)
{
    $query->where('title', 'like', "%{$keyword}%");
}

然后 URL 上使用 title=关键字 就能实现模糊查询了。

当然,你也可以定义模型中不存在的字段。

静默更新

我们有时候会想更新数据库中的记录,但是不希望触发 updated_at 更新,则可以在模型引入 App\Traits\QuietlySave 或者 App\Traits\QuietlyUpdate 这两个 trait:

// App\Traits\QuietlySave
User::saveQuietly([...]); 
// or
// App\Traits\QuietlyUpdate
User::updateQuietly([...]); 

内置接口

用户登录(获取 token)

POST /api/login
  • Request (application/json)
{
  "username": "admin",
  "password": "changeThis!!"
}
  • Response 200 (application/json)
{
    "token_type": "bearer",
    "token":"oVFV407i4jSTxjFO2tNxzh8lDaxVLbIkZZiDwjgMSYhvvkbUUXw8y0XgeYtxLAp4paznq0oxSMDdXmco"
}

用户注册

POST /api/register
  • Request (application/json)
{
   "username": "demo",
   "password": "123456"
}
  • Response 200 (application/json)
{
    "token_type": "bearer",
    "token":"oVFV407i4jSTxjFO2tNxzh8lDaxVLbIkZZiDwjgMSYhvvkbUUXw8y0XgeYtxLAp4paznq0oxSMDdXmco"
}

登出

POST /api/logout
  • Request (application/json) + Headers
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1...
  • Response 204

获取当前登录用户

GET /api/user
  • Request (application/json) + Headers
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1...
  • Response 200 (application/json)
{
  "id": "0892b118-856e-4a15-af0c-66a3a4a28eed",
  "username": "admin",
  "name": "超级管理员",
  "real_name": null,
  "avatar": "\/img\/default-avatar.png",
  "email": null,
  "gender": "none",
  "phone": null,
  "birthday": null,
  "status": "active",
  "cache": [],
  "properties": null,
  "settings": [],
  "is_admin": true,
  "last_active_at": null,
  "last_refreshed_at": null,
  "frozen_at": null,
  "status_remark": null,
  "email_verified_at": null,
  "created_at": "2020-03-17T09:37:45.000000Z",
  "updated_at": "2020-03-17T09:37:45.000000Z",
  "deleted_at": null
}

获取全局设置

GET /api/settings
  • Response 200 (application/json)
{
    "demo": {
        "status":"it works!"
    }
}

❤️ Sponsor me

If you like the work I do and want to support it, you know what to do ❤️

如果你喜欢我的项目并想支持它,点击这里 ❤️

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

License

MIT

More Repositories

1

pinyin

🇨🇳 基于词库的中文转拼音优质解决方案
PHP
4,296
star
2

share.js

一键分享到微博、QQ空间、QQ好友、微信、腾讯微博、豆瓣、Facebook、Twitter、Linkedin、Google+、点点等
JavaScript
3,691
star
3

easy-sms

📲 一款满足你的多种发送需求的短信发送组件
PHP
3,127
star
4

laravel-wechat

微信 SDK for Laravel, 基于 overtrue/wechat
PHP
2,865
star
5

socialite

Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
PHP
1,294
star
6

laravel-lang

🌏 75 languages support for Laravel application.
PHP
1,271
star
7

laravel-follow

❤️ This package helps you to add user based follow system to your model.
PHP
1,183
star
8

api.yike.io

一刻社区后端 API 源码
PHP
1,021
star
9

phplint

🐛 A tool that can speed up linting of php files by running several lint processes at once.
PHP
981
star
10

yike.io

一刻社区前端源码
Vue
658
star
11

laravel-pinyin

🇨🇳 Chinese to Pinyin translator for Laravel 5 / Lumen
PHP
529
star
12

chinese-calendar

📅 中国农历(阴历)与阳历(公历)转换与查询工具
PHP
521
star
13

laravel-versionable

⏱️Make Laravel model versionable
PHP
510
star
14

vue-avatar-cropper

👧 A simple and elegant avatar cropping and upload plugin.
JavaScript
508
star
15

laravel-filesystem-qiniu

A Qiniu Storage filesystem for Laravel
PHP
471
star
16

laravel-like

👍 User-like features for Laravel Application.
PHP
467
star
17

laravel-favorite

❤️ User favorite feature for Laravel Application.
PHP
422
star
18

laravel-query-logger

📝 A dev tool to log all queries for laravel application.
PHP
408
star
19

laravel-shopping-cart

🛒 Shopping cart for Laravel Application.
PHP
392
star
20

laravel-ueditor

UEditor integration for Laravel.
JavaScript
390
star
21

laravel-socialite

:octocat: Social OAuth Authentication for Laravel 5. drivers: facebook, github, google, linkedin, weibo, qq, wechat and douban
PHP
339
star
22

latest-laravel

【不再更新】这些东西本不应该存在。
Shell
282
star
23

weibo-dogs-killer

一段屏蔽 HWB(微博监督员) 的 js 小脚本
275
star
24

wisteria

Beautiful document tool for your project.
Blade
255
star
25

flysystem-qiniu

💾 Flysystem adapter for the Qiniu storage.
PHP
221
star
26

package-builder

📦 A composer package builder.
PHP
199
star
27

weather

🌈 基于高德开放平台接口的 PHP 天气信息组件。
PHP
173
star
28

laravel-subscribe

📧 User Subscribe/Unsubscribe features for Laravel Application.
PHP
170
star
29

yike

PHP
164
star
30

laravel-emoji

😄 This package assist you in getting started with emoji easily.
PHP
151
star
31

vscode-miniapp-helper

微信小程序开发助手 for VSCode
JavaScript
150
star
32

yaf-skeleton

The Yaf testable skeleton and composer supported.
PHP
141
star
33

laravel-uploader

🌴 An upload component for Laravel.
PHP
135
star
34

http

🌵 A simple http client wrapper.
PHP
126
star
35

writor

基于 Laravel 4 开发的博客系统
JavaScript
125
star
36

pinyin-resources

汉字拼音相关参考资料
123
star
37

laravel-vote

⬆️ ⬇️ User vote system for Laravel Application.
PHP
108
star
38

validation

Laravel Validation 简化无依赖版
PHP
100
star
39

bootstrap-theme-slim

纤细风格的bootstrap主题
HTML
92
star
40

city.js

城市选择控件
92
star
41

laravel-payment

Omnipay ServiceProvider for Laravel.
PHP
91
star
42

laravel-passport-cache-token

Make laravel/passport token cacheable.
PHP
89
star
43

laravel-filesystem-cos

Tencent Cloud COS storage for Laravel based on overtrue/flysystem-cos.
PHP
89
star
44

php-opencc

中文简繁转换,支持词汇级别的转换、异体字转换和地区习惯用词转换(中国大陆、台湾、香港、日本新字体)。
PHP
75
star
45

blog

安正超博客
Less
74
star
46

flysystem-cos

💾 Flysystem adapter for the Qcloud COS storage.
PHP
74
star
47

rester

基于 Slim+Eloquent 的 RESTful API 框架
PHP
71
star
48

laravel-mail-aliyun

📧 Aliyun DrirectMail Transport for Laravel Application.
PHP
71
star
49

laravel-youzan

【停止维护】Youzan wrapper for Laravel
PHP
59
star
50

websocket

A PHP implementation of WebSocket.
PHP
56
star
51

laravel-options

Global options module for Laravel application.
PHP
51
star
52

laravel.xyz

Source code of https://laravel.xyz
Vue
50
star
53

json-viewer

A tool for make JSON view in browser.
JavaScript
49
star
54

building-with-tailwindcss

HTML
45
star
55

php-package

A PHP package template repository.
45
star
56

laravel-qcloud-content-audit

腾讯云内容安全(文字图片内容审核)服务
PHP
44
star
57

phpmd-rulesets

PHP_MD 规则
42
star
58

sketch-data-cn

为 Sketch 准备的模拟数据中文版,包含:中文姓名,手机号,省份,城市,地区,公司名,银行名,星期几,详情地址,邮编,邮箱,颜色,广告词等。
41
star
59

qcloud-cos-client

Tencent COS Client
PHP
40
star
60

validator.js

一个类似laravel的js验证模块.
JavaScript
39
star
61

translator

PHP多语言支持工具
PHP
38
star
62

laravel-payable

Payment system for Laravel.
PHP
36
star
63

laravel-package

Laravel package template
PHP
35
star
64

laravel-revaluation

Laravel 5 model revaluation helper.
PHP
31
star
65

laravel-easy-sms

overtrue/easy-sms service provider for Laravel.
PHP
26
star
66

laravel-saml

SAML toolkit for Laravel based on OneLogin's SAML PHP Toolkit.
PHP
21
star
67

sendcloud

SendCloud Mail SDK
PHP
20
star
68

laravel-sendcloud

SendCloud Mail SDK for Laravel.
PHP
20
star
69

latest-lumen

【不再更新】这些东西本不应该存在。
Shell
19
star
70

laravel-stateless-session

A lightweight middleware to make api routing session capable.
PHP
18
star
71

laravel-single-session

A plugin provide single session authentication for Laravel 5.
PHP
17
star
72

php-multi-process-runner

基于PHP拓展PCNTL的多进程执行工具
PHP
17
star
73

laravel-summernote

Summernote editor integration for Laravel 5.
JavaScript
16
star
74

laravel-passport-cache-client

Make laravel/passport client cacheable.
PHP
16
star
75

laravel-qcloud-federation-token

QCloud COS FederationToken generator for Laravel.
PHP
16
star
76

cuttle

📃 A multi-module log wrapper.
PHP
15
star
77

stuq-laravel-course

StuQ小班课|Laravel实战经验分享及PHP后端编程思想
PHP
13
star
78

laravel.so

Source Code of http://laravel.so (old)
PHP
13
star
79

double-array-trie

PHP
12
star
80

overtrue

https://overtrue.me
12
star
81

bash-color

Generate command line colorized text.
PHP
12
star
82

pinyin-dictionary-maker

📇 Dictionary make of overtrue/pinyin.
PHP
12
star
83

relocator.js

relocate images to fit image box
JavaScript
11
star
84

laravel-open-telemetry

This package provides a simple way to add OpenTelemetry to your Laravel application.
PHP
11
star
85

youzan

【不再维护】Youzan SDK.
PHP
10
star
86

wisteria-skeleton

The skeleton of Wisteria.
PHP
10
star
87

wp-cn-excerpt

wordpress中文摘要插件
PHP
10
star
88

laravel-custom-log

📃 Make Laravel Log great again.
PHP
9
star
89

laravel-qcloud-captcha

QCloud Captcha service validator for Laravel.
PHP
7
star
90

vue-avatar-cropper-demo

Created with CodeSandbox
Vue
7
star
91

string2pinyin.sinaapp.com

PHP
3
star
92

nuxt-dashboard

CSS
3
star
93

wp-auto-top

WordPress 返回顶部插件
PHP
2
star
94

redius.xyz

wip
2
star
95

overtrue.github.io

HTML
2
star
96

username-blacklist

PHP
2
star
97

pinyin.rs

2
star
98

easywechat-theme

Vue
2
star
99

dex-swap-test

JavaScript
1
star
100

phplint-test

PHP
1
star