承接 matt-daneshvar/rest 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

matt-daneshvar/rest

Composer 安装命令:

composer require matt-daneshvar/rest

包简介

General implementation for Laravel 5 resource controllers

README 文档

README

Packagist Version Build Status GitHub

General implementation for Laravel resource controllers.

By assuming a few conventions this package takes care of the repetitive implementation of the CRUD (create, read, update, and delete) operations on a base controller which you can fully customize and extend to your liking.

Installation

Require the package using composer:

composer require matt-daneshvar/rest

Usage

Extend the ResourceController and specify a $resource. Optionally define a $rules property to enforce validation before store and update operations.

<?php

use App\Task;
use MattDaneshvar\ResourceController\ResourceController;

class TasksController extends ResourceController
{
  protected $resource = Task::class;

  protected $rules = ['name' => 'required|max:200'];
}

The TasksController in the example above will now have general implementation for all CRUD actions. That includes:

  • A create method that returns the tasks.create view.
  • A show method that returns the tasks.show view injected with the relevant Task model.
  • A create method that returns the tasks.create view.
  • A store method that validates* the request against $rules, persists a Task object, and redirects user back to the url corresponding to the TasksController@index action.
  • An edit method that returns the tasks.edit view injected with the relevant Task model.
  • An update method that validates* the request against $rules, updates the relevant Task object, and redirects user back to the url corresponding to the TasksController@index action.
  • A destory method that deletes the relevant Task object, and redirects user back to the url corresponding to the TasksController@index action.

*should the validation fail, user is redirected back() with all inputs and errors flashed to the session.

Routing

This package is designed to match Laravel's resource controllers. Creating a route rule for this controller is simple:

Route::resource('tasks', 'TasksController');

Limiting the exposed CRUD operations

While extending ResourceController automatically includes all CRUD operations in your controller, you may wish to limit the available operations available for your specific resource. You could achieve that by limiting the routes you expose in your application:

Route::resource('tasks', 'TasksController', ['only' => ['index', 'show']]);

Validation

Most real-world applications would validate store and update requests before persisting anything to the database.

Specifying a $rules property on your controller, informs the ResourceController to validate user's request against those rules in both store and update operations.

class TasksController extends ResourceController
{
  protected $resource = Task::class;

  protected $rules = ['name' => 'required|max:200'];
}

If you wish to use different rules for store and update operations, you may specify them separately:

class TasksController extends ResourceController
{
  protected $resource = Task::class;

  protected $storeRules = ['name' => 'required|max:200'];
  
  protected $updateRules = ['name' => 'required|max:200'];
}

Pagination

In most situations, your application's index would use pagination instead of dumping your models into the view all at once. The ResourceController assumes 20 items per page. To change this number define the $perPage attribute, or set it to null to disable pagination altogether.

class TasksController extends ResourceController
{
  protected $resource = Task::class;

  protected $perPage = 10;
}

Other configurations

While assuming a set of default conventions with sensible defaults to minimize code repetition in most cases, this package is also packed with a bunch of configuration options to allow you override the default behaviour when needed.

class TasksController extends ResourceController
{
  /**
   * The resource class name.
   *
   * @var string
   */
  protected $resource = '';

  /**
   * The number of models to return for pagination.
   *
   * @var int|null
   */
  protected $perPage = 20;

  /**
   * Path to all views for this controller (dot-separated path from views directory).
   *
   * @var string
   */
  protected $viewsPath = null;

  /**
   * Views for different resource actions.
   *
   * @var array
   */
  protected $views = [];

  /**
   * Validation rules.
   *
   * @var array
   */
  protected $rules = [];

  /**
   * Validation rules for store action.
   *
   * @var array
   */
  protected $storeRules = null;

  /**
   * Validation rules for update action.
   *
   * @var array
   */
  protected $updateRules = null;
}

License

The MIT License (MIT). Please see License File for more information.

matt-daneshvar/rest 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 17
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-14