定制 origami/api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

origami/api

Composer 安装命令:

composer require origami/api

包简介

API Laravel 5 package

README 文档

README

This package simplifies the process of setting up an API using Laravel 6. Many of the practices utilised in this package are courtesy of Build APIs you Won't Hate by Phil Sturgeon.

Installation

Install this package through Composer.

composer require origami/api

Requirements

This package is designed to work with Laravel >= 6 currently.

Configuration

There are some API configuration options that you'll want to overwrite. First, publish the default configuration.

php artisan vendor:publish

This will add a new configuration file to: config/api.php.

<?php
return array(

	'keys' => [
		env('API_KEY', 'secret')
	],

);

keys

This is the valid list of API keys that authenticate requests. By default we support an environment variable of API_KEY which you can set in your .env file.

Middleware

This package includes two Middleware classes for Laravel 6

Origami\Api\Middleware\AuthenticateApiKey

The AuthenticateApiKey Middleware is designed to guard Api routes against unauthorised access. We recommend you include it on all routes as follows, unless you have a public API.

Route::group(['prefix' => 'api/v1', 'namespace' => 'Api', 'middleware' => 'Origami\Api\Middleware\AuthenticateApiKey'], function()
{
	Route::get('tasks/{id}', 'Tasks@show');
	Route::post('tasks/{id}', 'Tasks@update');
});

Controllers

We provide a helpful ApiController base controller class that includes a response method, allowing you to return json responses or get access to the Origami\Api\Response class which offers a variety of helpers methods.

Responses

The Origami/Api/Response class offers a variety of helper methods and ultimately uses the Illuminate\Contracts\Routing\ResponseFactory Laravel class to return a json response with appropriate headers.

You can use the API Response class in your controller by using the response helper method:

	public function index()
	{
		$items = new Collection(['one','two','three']);

		// Calling with a single argument returns a json response
		return $this->response($items);
	}

or

	public function index()
	{
		$items = new Collection(['one','two','three']);

		// Calling with no argument returns the response object
		return $this->response()->data($items);
	}

	public function find($id)
	{
		$item = Item::find($id);

		if ( ! $item ) {
			// Using the response object you can call helper methods.
			return $this->response()->errorNotFound();
		}

		return $this->response()->data($item);
	}

Items or Collections

We make use of the excellent league/fractal PHP package to parse and transform Eloquent models and collections. For more information visit fractal.thephpleague.com

There are three helper methods on the response object

  • resourceItem
  • resourceCollection
  • resourcePaginator

Full Example

Routes: app/Http/routes.php

<?php
	Route::group(['prefix' => 'api/v1', 'namespace' => 'Api', 'middleware' => 'Origami\Api\Middleware\AuthenticateApiKey'], function()
{
    Route::get('items', 'Items@index');
    Route::get('tasks/{id}', 'Tasks@show');
});

Controller: app/Http/Controllers/Api/Items.php

<?php namespace App\Http\Controllers\Api;

use Origami\Api\ApiController;
use App\Items\Item; // Assuming an eloquent Model;
use App\Items\ItemTransformer;

class Items extends ApiController {

	public function index()
	{
		$items = Item::paginate(15);

		return $this->response()->resourcePaginator($items, new ItemTransformer);
	}

	public function show($id)
	{
		$item = Item::find($id);

		if ( ! $item ) {
			return $this->response()->errorNotFound('Item not found');
		}

		return $this->response()->resourceItem($item, new ItemTransformer);
	}

}

Transformer: app/Items/ItemTransformer.php

<?php namespace App\Items;

use Origami\Api\Transformer;

class ItemTransformer extends Transformer {

    public function transform(Item $item)
    {
        return [
            'id' => $item->id,
            'title' => $task->title,
            'created_at' => $task->created_at->toDateTimeString()
        ];
    }

}

License

View the license

origami/api 适用场景与选型建议

origami/api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.43k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 origami/api 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.43k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-23