milkmeowo / starter-framework
Laravel / Lumen 快速启动框架。
Requires
- php: >=5.6.4
- barryvdh/laravel-cors: ^0.8.6
- barryvdh/laravel-ide-helper: ^2.3
- dingo/api: 1.0.x@dev
- doctrine/dbal: ^2.5
- dusterio/lumen-passport: ^0.1.9
- itsgoingd/clockwork: ^1.13
- league/fractal: ^0.15.0
- overtrue/wechat: ^3.2
- prettus/l5-repository: ^2.6
Requires (Dev)
- laravel/framework: 5.4.*
- laravel/lumen-framework: 5.4.*
- phpunit/phpunit: ~5.7
This package is auto-updated.
Last update: 2024-09-23 20:53:49 UTC
README
概述
此软件包适用于面向 Restful API 的项目。
作为后端层的 Restful API 为前端(Web 和移动应用)提供简单的统一接口。
它使用 Laravel Passport 进行受保护资源的认证。
它完全利用了仓库设计模式。
特性
- Dingo Api 支持
- l5-Repository 支持
- Laravel Passport 支持
- ide-helper 支持
- clockwork 支持
- Cors 支持
- 以上所有功能均支持 Laravel 和 Lumen。
入门指南
通过 Composer 安装
composer require milkmeowo/starter-framework
注册 ServiceProvider
通过将其添加到 providers 数组中,注册 starter-framework 服务提供者。
Laravel
'providers' => array(
...
Milkmeowo\Framework\Base\Providers\LaravelServiceProvider::class,
)
Lumen
修改引导流程(bootstrap/app.php 文件)
// Enable Facades
$app->withFacades();
// Enable Eloquent
$app->withEloquent();
$providers = [
...
Milkmeowo\Framework\Base\Providers\LumenServiceProvider::class,
];
array_walk($providers, function ($provider) use ($app) {
$app->register($provider);
});
发布配置
然后复制配置文件到您的项目。您可以手动完成此操作
cp -R vendor/milkmeowo/starter-framework/config ./config
或者对于 Laravel,您可以使用
php artisan vendor:publish
并更改 config/auth.php
api guards 驱动到 passport
'guards' => [
...
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
迁移并安装 Laravel Passport
# Create new tables for Passport
php artisan migrate
# Install encryption keys and other necessary stuff for Passport
php artisan passport:install
软件包文档
Dingo
改进
异常处理注册
将 src/Framework/Dingo/Providers/ExceptionHandlerServiceProvider
添加到注册通用异常处理。
- Illuminate\Auth\Access\AuthorizationException
- Illuminate\Auth\AuthenticationException
- Illuminate\Database\Eloquent\ModelNotFoundException
- Illuminate\Http\Exception\HttpResponseException
- League\OAuth2\Server\Exception\OAuthServerException
- Prettus\Validator\Exceptions\ValidatorException
ResponseWasMorphed 事件监听器
触发 src/Framework/Dingo/Listeners/AddPaginationLinksToResponse
身份验证提供者
添加 Passport
和改进的 Oauth2
身份验证提供者
您可以使用配置进行使用
// file config/api.php
...
'auth' => [
Milkmeowo\Framework\Dingo\Auth\Providers\OAuth2::class,
Milkmeowo\Framework\Dingo\Auth\Providers\Passport::class,
],
...
参考
l5-Repository
改进
artisan 命令
// example for generate Post Entity Repository
php artisan starter:entity Post
自动生成以下文件
modified: routes/api.php
modified: app/Providers/RepositoryServiceProvider.php
add: app/Api/Controllers/V1/PostsController.php
add: app/Models/Post.php
add: app/Presenters/PostPresenter.php
add: app/Repositories/Criteria/PostCriteria.php
add: app/Repositories/Eloquent/PostRepositoryEloquent.php
add: app/Repositories/Interfaces/PostRepository.php
add: app/Transformers/PostTransformer.php
add: app/Validators/PostValidator.php
add: database/migrations/2017_01_12_091412_create_posts_table.php
add: database/seeds/PostSeeder.php
- 控制器(
index
store
show
update
destory
trashedIndex
trashedShow
restore
) - 模型(BaseModel 带有 ObserveEvent 自动跟踪记录修改,使用
create(update/delete)_at(by/ip)
) - 表示器
- 转换器(
league/fractal
) - 仓库/标准
- 仓库/Eloquent(BaseEloquent 带有 ObserverEvents,Cacheable,SoftDeletes 支持)
- 仓库/接口
- 验证器(prettus/laravel-validator 支持)
- 迁移(new blueprint methods recordStamps[
create(update)_at(by/ip)
] softDeletesRecordStamps[delete_at(by/ip)
]) - 播种器
- 附加路由(文件
routes/api.php(web.php)
,带有占位符//:end-routes:
可以自动注入 Restful API) - 绑定仓库接口和 Eloquent
路由端点
文件 routes/api.php
,带有占位符 //:end-routes:
<?php
$api = app('Dingo\Api\Routing\Router');
// v1 version API
// choose version add this in header Accept:application/vnd.lumen.v1+json
$api->version('v1', [
'namespace' => 'App\Api\Controllers\V1',
], function ($api) {
/*
|------------------------------------------------
| User Routes
|------------------------------------------------
*/
// trashed listing
$api->get('/users/trashed', ['as' => 'users.trashed.index', 'uses' => 'UsersController@trashedIndex']);
// show trashed special resource
$api->get('/users/trashed/{id}', ['as' => 'users.trashed.show', 'uses' => 'UsersController@trashedShow']);
// restore
$api->put('/users/{id}/restore', ['as' => 'users.restore', 'uses' => 'UsersController@restore']);
// listing
$api->get('/users', ['as' => 'users.index', 'uses' => 'UsersController@index']);
// create
$api->post('/users', ['as' => 'users.store', 'uses' => 'UsersController@store']);
// show
$api->get('/users/{id}', ['as' => 'users.show', 'uses' => 'UsersController@show']);
// update
$api->put('/users/{id}', ['as' => 'users.update', 'uses' => 'UsersController@update']);
// delete
$api->delete('/users/{id}', ['as' => 'users.destroy', 'uses' => 'UsersController@destroy']);
//:end-routes:
});
ObserveEvent
模型
和仓库/Eloquent
监听以下方法事件,$priority = 99
- onCreating()
- onCreated()
- onUpdating()
- onUpdated()
- onSaving()
- onSaved()
- onDeleting()
- onDeleted()
- onRestoring()
- onRestored()
模型还具有默认记录跟踪事件监听器
- onCreating()
// auto set related user id
if ($this->autoRelatedUserId && empty($this->{static::RELATED_USER_ID}) && $this->hasTableColumn(static::RELATED_USER_ID)) {
$user_id = $this->getAuthUserId();
if ($user_id > 0) {
$this->{static::RELATED_USER_ID} = $user_id;
}
}
- onSaving()
// update ipstamps if true
if ($this->ipstamps) {
$this->updateIps();
// update userstamps if true
if ($this->userstamps) {
$this->updateUsers();
}
protected function updateIps()
{
$ip = smart_get_client_ip();
if (! $this->isDirty(static::UPDATED_IP) && $this->hasTableColumn(static::UPDATED_IP)) {
$this->{static::UPDATED_IP} = $ip;
}
if (! $this->exists && ! $this->isDirty(static::CREATED_IP) && $this->hasTableColumn(static::CREATED_IP)) {
$this->{static::CREATED_IP} = $ip;
}
}
protected function updateUsers()
{
$user_id = $this->getAuthUserId();
if (! ($user_id > 0)) {
return;
}
if (! $this->isDirty(static::UPDATED_BY) && $this->hasTableColumn(static::UPDATED_BY)) {
$this->{static::UPDATED_BY} = $user_id;
}
if (! $this->exists && ! $this->isDirty(static::CREATED_BY) && $this->hasTableColumn(static::CREATED_BY)) {
$this->{static::CREATED_BY} = $user_id;
}
}
- onDeleting()
if (static::usingSoftDeletes()) {
if ($this->hasTableColumn(static::DELETED_BY)) {
$this->{static::DELETED_BY} = $this->getAuthUserId();
}
if ($this->hasTableColumn(static::DELETED_IP)) {
$this->{static::DELETED_IP} = smart_get_client_ip();
}
$this->flushEventListeners();
$this->save();
}
- onRestoring()
if ($this->hasTableColumn(static::DELETED_BY)) {
$this->{static::DELETED_BY} = null;
}
if ($this->hasTableColumn(static::DELETED_IP)) {
$this->{static::DELETED_IP} = null;
}
仓库/Eloquent
-
SoftDeletes 支持
- withTrashed
- withoutTrashed
- onlyTrashed
- restore
- restoreWhere
- ForceDelete
- forceDeleteWhere
参考
Illuminate 数据库
改进
- 支持全文索引
- 添加一组蓝图方法
- 添加 ipStamps 方法(
create_ip
update_ip
) - 添加 userStamps 方法(
create_by
update_by
) - 添加 recordStamps 方法(
timestamps
ipStamps
userStamps
) - 添加 softDeletesStamps 方法(
delete_ip
delete_by
) - 添加 softDeletesRecordStamps 方法(
softDeletes
softDeletesStamps
) - 添加 dropIpStamps 方法
- 添加 dropUserStamps 方法
- 添加 dropRecordStamps 方法
- 添加 dropSoftDeletesStamps 方法
- 添加 dropSoftDeletesRecordStamps 方法
- 添加 ipStamps 方法(
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->recordStamps();
$table->softDeletesRecordStamps();
$table->text('content')->fulltext()
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
laravel passport
Laravel
参考
官方文档.
Lumen
已安装的路由
添加此服务提供者将挂载以下路由
请注意,由于Laravel Passport的一些路由与Web相关且依赖于会话(例如授权页面),因此它们不得不“消失”。Lumen是一个API框架,因此仅存在与API相关的路由。
参考
Ide-helper
当
APP_ENV != production
时注册服务提供者
Laravel Facades 的自动 phpDoc 生成
php artisan ide-helper:generate
模型的自动 phpDocs
php artisan ide-helper:models
Container 实例的 PhpStorm Meta
php artisan ide-helper:meta
参考
Clockwork
当
APP_DEBUG = true
且APP_ENV != production
时注册服务提供者
参考
Cors
使用方法
ServiceProvider 添加了一个名为 cors
的路由中间件,您可以使用它来添加 CORS 支持。
Route::group(['middleware' => 'cors'], function(Router $router){ $router->get('api', 'ApiController@index'); });
如果您希望 CORS 应用于所有路由,请在 app/http/Kernel.php
中将其添加为全局中间件。
protected $middleware = [ .... \Barryvdh\Cors\HandleCors::class ];
参考
许可证
starter-framework 是一个开源软件,根据 MIT 许可证 许可。