定制 osedea/laravel-rest 二次开发

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

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

osedea/laravel-rest

Composer 安装命令:

composer require osedea/laravel-rest

包简介

A bundle providing a RESTful API for the Laravel framework version 5 using the command bus.

README 文档

README

This package is no longer maintained. Should you have any issues, you are free to fork the repo.

LaravelRest

This is a Laravel 5 package that creates RESTful API routes for your models and uses the command bus to execute CRUD requests.

Installation

Install the package via composer:

composer require osedea/laravel-rest:~0.2.1

Usage

First, add the eloquence service provider to your config/app.php file:

'Osedea\LaravelRest\LaravelRestServiceProvider'

All you models need to use the trait \Osedea\LaravelRest\Traits\CommandModel to provide some attributes and methods:

<?php

namespace Acme;

class SomeModel {
    use \Osedea\LaravelRest\Traits\CommandModel;
}

Then, publish the config file to your application:

`php artisan vendor:publish --provider="Osedea\LaravelRest\LaravelRestServiceProvider"`

You should now be good to go without having to change anything! Access /api/v1/users to see a list of users!

Config Options

Application Namespace

You need to specify the application namespace used in your project. The default is App, but if you changed it, you need to change it in the config:

<?php

return [
    'app_namespace' => 'SomethingElse'
];

Models Namespace

If you models are in a custom namespace different from app_namespace, you can specify this config.

<?php

return [
    'models_namespace' => 'SomethingElse'
];

Mapping

To add a resource to the default controller, you just have to add a line in the /config/api.php file:

<?php

return [
    'mapping' => [
        'users' => 'User',
    ]
];

API Prefix

If you want to use a different prefix, you can do so by specifying the prefix key:

<?php

return [
    'prefix' => 'api/v2'
];

If you don't specify the key prefix, it will default to api.

Commands and the default controller

LaravelRest comes with a default REST controller to handle resources. This allows automatic actions on resources listing, showing, deleting and lising relations.

Actions

Method Url Command Description Options
GET /{resource} Index List resources page, perPage, sort, fields, embed
GET /{resource}/{id} Show Show one resource fields, embed
POST /{resource} Store Create a resource
PUT /{resource}/{id} Update Update a resource
DELETE /{resource}/{id} Destroy Remove a resource
GET /{resource}/{id}/{relation} RelationIndex List a resource relation page, perPage, sort, fields, embed

Options

Pagination

All lists are paginated by default. You can set a number of items per page for each Model by overriding a variable:

protected $perPage = 10;

You can also define a maximum for this value because it can be overridden in a request:

protected $perPageMax = 50;

And then in a request: /users?page=2&perPage=3

Sorting

Sorting can be done on multiple columns by separating them with commas:

/users?sort=name,email

You can choose the order (desc or asc) by putting a - in front of a column name:

/users?sort=-id

Fields

If you don't need all fields when querying a list or a resource, you can specify which fields you need:

/users?fields=id,name

Nesting

Use this only when you REALLY need to.

If you absolutely need nested relations, you specify them like so:

/users?embed=groups

Commands

Defaults

LaravelRest has default commands for several actions:

  • GET /{resource}: Osedea\LaravelRest\Commands\DefaultCommand\IndexCommand
  • GET /{resource}/{id}: Osedea\LaravelRest\Commands\DefaultCommand\ShowCommand
  • DELETE /{resource}/{id}: Osedea\LaravelRest\Commands\DefaultCommand\DeleteCommand
  • GET /{resource}/{id}/{relation}: Osedea\LaravelRest\Commands\DefaultCommand\RelationIndexCommand

With these defaults, the mapping is enough for them to work. You can override them by creating a command using the correct namespace:

Example, to override the POST /users/{id} default command, create one called Osedea\LaravelRest\Commands\UserCommand\DestroyCommand.

Create and Update

Those actions are very specific to a resource so there is no default.

You will have to create them for all resources listed in the mapping file. The schema is this:

Osedea\LaravelRest\Commands\{mapping[resource]}Command\{action}Command

For example, to add the create command of the users resource, the command is:

Osedea\LaravelRest\Commands\UserCommand\StoreCommand

A command class

A command class needs to extend Osedea\LaravelRest\Commands\Command and implement Illuminate\Contracts\Bus\SelfHandling.

osedea/laravel-rest 适用场景与选型建议

osedea/laravel-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 145 次下载、GitHub Stars 达 19, 最近一次更新时间为 2015 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 osedea/laravel-rest 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 19
  • Watchers: 28
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-05-14