swisnl/json-api-server
最新稳定版本:0.6.0
Composer 安装命令:
composer require swisnl/json-api-server
包简介
Set up a JSON API in Laravel in just a few minutes.
README 文档
README
🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
We don't use this package anymore in our own projects and there are many, better alternatives, so that's why we have chosen to abandon it. We suggest using laravel-json-api/laravel or one of the other alternatives instead. Feel free to fork our code and maintain your own copy.
Laravel JSON API server
Set up a laravel API in just a few minutes with this package. All the standard API functionality is already there for you.
This package strives to save you some time while building your API. It already has the basic features an API should have, like:
- Generators to generate your needed files for each model
- An abstract layer to handle your basic CRUD actions
- Creates routes for all your endpoints
- Support for a few useful URL parameters
- Permission and route permission handling
- Responses in json api format (http://jsonapi.org/)
- Automatically translates your models based on your database
Install
Via Composer
$ composer require swisnl/json-api-server
Sample
Please see the folder sample for a sample of an application using this package.
Usage
Base Api Classes
There are a few base classes your classes should/could inherit:
BaseApiController
This controller handles your basic CRUD actions as well as permissions if you choose to use permissions.
BaseApiRepository
This repository is a standard base repository with a small addition that it figures out your models relationships.
If you want to use your own BaseRepository, you have to implement the RepositoryInterface. This ensures that you have full compatibility with the BaseApiController.
The BaseApiRepository uses a trait to retrieve a models relationships. You can use this trait if you want to use the existing implementation.
Generating the required files
After installing the package you can instantly generate all the required files by executing this command:
$ php artisan json-api-server:generate-all {Model}
To override the default path without overriding the laravel_generator config file, you can use the --path={path} option. For example:
$ php artisan json-api-server:generate-all Test --path=app/temp/
This generates the following files:
- An eloquent model
- A translation model
- An API controller
- Should extend the BaseApiController
- A routes file where the all the CRUD endpoints are defined
- A repository for your model
- Could extend the BaseApiRepository
- A policy for checking permissions
- 1 test for checking if a user has permissions for the endpoint
You'll be able to do the basic CRUD actions without writing anything.
You also have the ability to generate the files separately:
$ php artisan json-api-server:generate-controller {name}
$ php artisan json-api-server:generate-model {name}
$ php artisan json-api-server:generate-model-permissions {name}
$ php artisan json-api-server:generate-policy {name}
$ php artisan json-api-server:generate-repository {name}
$ php artisan json-api-server:generate-routes {name}
$ php artisan json-api-server:generate-test {name}
$ php artisan json-api-server:generate-translation {name}
Configuration
If you would like to override the configuration files.
$ php artisan vendor:publish --tag=laravel-api $ php artisan vendor:publish --tag=laravel-api-templates
If you decide to override the templates, make sure you override the laravel api config too. You have to define where your own templates are in the config.
This is the default configuration:
return [ // Generator configuration 'path' => [ 'model' => app_path('/'), 'model_permissions' => app_path('Permissions/'), 'translation' => app_path('Translations/'), 'controller' => app_path('Http/Controllers/Api/'), 'repository' => app_path('Repositories/'), 'policy' => app_path('Policies/'), 'auth_test' => base_path('tests/Authentication/'), 'templates' => 'vendor/swisnl/laravel-api/resources/templates/', 'routes' => app_path('Http/Routes/') ], 'namespace' => [ 'model' => 'App', 'model_permissions' => 'App\Permissions', 'controller' => 'App\Http\Controllers\Api', 'repository' => 'App\Repositories', 'translation' => 'App\Translations', 'policy' => 'App\Policies', 'auth_test' => 'App\Tests\Authentication' ], // Permissions configuration 'permissions' => [ 'checkDefaultIndexPermission' => false, 'checkDefaultShowPermission' => false, 'checkDefaultCreatePermission' => false, 'checkDefaultUpdatePermission' => false, 'checkDefaultDeletePermission' => false, ], // Load all relationships to have response exactly like json api. This slows down the API immensely. 'loadAllJsonApiRelationships' => true, ];
Requests and responses
All requests and responses are formatted according to the format specified by http://jsonapi.org/.
There are several respond methods at your disposal in your controller. The following respond methods are implemented at this moment:
return $this->respondWithOk($object); return $this->respondWithPartialContent($object); return $this->respondWithCreated($object); return $this->respondWithNoContent($object); return $this->respondWithCollection($object);
These methods automatically converts your objects to json api format and creates a response with the correct status code and body.
Using policies
If you decide to use policies to check for the user's pemissions you have to add the policies to your Providers\AuthServiceProvider.
protected $policies = [ Sample::class => SamplePolicy::class, ]; public function boot() { $this->registerPolicies(); }
The policies are preconfigured to work with Laravel passport, if you want to use another form of authorizing actions you can change the methods.
If you want to redirect the validation to a specific function in your policy.
$this->authorizeAction('show');
If you want to check if they can request a specific object you can add that object as the second parameter:
$this->authorizeAction('show', $requestedObject);
URL parameters out of the box
The following URL parameters are supported after installing this package:
- ?include={relationship}: To add all relationship data to the response.
- ?page={pageNumber}: To decide which page the pagination should show.
- ?per_page={amountToShowPerPage}:To decide how many items you get per page.
- ?ids={commaSeperatedIds}: To retrieve a collection of objects belonging to the ids.
- ?exclude_ids={commaSeperatedIds}: To retrieve a collection of objects not belonging to the ids.
- ?lang={language}: (Requires the configure-locale middleware) to change the php locale to the desired language and automatically translates all translatable models.
- ?fields={columns}: To retrieve certain columns.
- ?order_by_desc={column}: To order descending based on a column.
- ?order_by_asc={column}: To order ascending based on a column.
Mandatory middleware
- inspect_content_type: Required. It ensures that the requests should be in json format. If it's in another format it throws a ContentTypeNotSupportedException.
Optional middleware
There is optional middleware:
- configure-locale: used to configure the language for translating your responses. Also configurable by using the URL paramater ?lang={language}
Passport installation
Note: if you want to know more about laravel passport and why these commands should be run go to https://laravel.com/docs/passport
$ composer require laravel/passport $ php artisan migrate $ php artisan passport:install
After running this command, add the Laravel\Passport\HasApiTokens trait to your App\User model
Next, you should call the Passport::routes method within the boot method of your AuthServiceProvider.
Finally, in your config/auth.php configuration file, you should set the driver option of the api authentication guard to passport. This will instruct your application to use Passport's TokenGuard when authenticating incoming API requests.
If you created your own routes make sure you have the auth:api middlware on all the routes you want to use passport with.
Packages Laravel-Api uses
Laravel framework
Astrotomic / laravel-translatable
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email security@swis.nl instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
SWIS
SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.
swisnl/json-api-server 适用场景与选型建议
swisnl/json-api-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.85k 次下载、GitHub Stars 达 104, 最近一次更新时间为 2018 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「JSON-API」 「laravel-api」 「swisnl」 「json-api-server」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 swisnl/json-api-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 swisnl/json-api-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 swisnl/json-api-server 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP-code generator (based on OAS) for Laravel framework, with complete support of JSON-API data format
The jsonapi-querifier-bundle offers the Querifer filter syntax for the paknahad/jsonapi-bundle.
LarapiKit is a versatile and robust Laravel API package designed to simplify and enhance your web application development process. With a focus on security time saving and easy integration, this package empowers developers to streamline their API-related tasks, saving time and effort.
Argonaut is a lightweight Data Transfer Object (DTO) package for Laravel that supports nested casting, recursive serialization, and validation out of the box. Ideal for service layers, APIs, and clean architecture workflows.
The jsonapi-bundle is a Symfony bundle. it is the fastest way to generate API based on JsonApi.org using woohoolabs/yin
Easily build Eloquent queries from API requests using Alchemist Restful API.
统计信息
- 总下载量: 15.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 104
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-12