engency/resource-controllers
Composer 安装命令:
composer require engency/resource-controllers
包简介
Resource Controllers for Laravel
关键字:
README 文档
README
Don't waste valuable time on writing basic CRUD operations for your Laravel application.
Ideology
Most CRUD operations are very straight-forward, especially when conforming to the REST design. As long as a few basic rules are met, generic logic can do the trick.
- Middleware authorizes users for operations on resources
- The controller determines the scope
- Rules validate input before storing and updating resources
- Rules make sure clients will only receive the attributes they are authorized to see
For applications with both an API and 'general' webinterface, you ideally just want a single controller performing the basic crud operations on a resource. Therefore, generic logic should be able to construct responses in both HTML and JSON format.
Requirements
- PHP 7.1+ | PHP 8+
- The Laravel framework
Installation
composer require engency/laravel-resource-controller
Alongside this package, the engency/eloquent-formatting and engency/laravel-model-validation package will be installed.
Usage
The most basic setup could look as following;
The controller
use Illuminate\Http\Request; use Engency\Http\Controllers\ResourceController; use Engency\Http\Controllers\DefaultResourceActions; class UserController extends ResourceController { use DefaultResourceActions; /** * Provide the resource class in the parent's constructor. * Add any middleware to authorize users. */ public function __construct() { parent::__construct(User::class); $this->middelware('auth'); } /** * Set the scope for this resource controller. * The expected return value should either be a query builder or a Laravel collection. * * @param Request $request * @return \Illuminate\Database\Query\Builder|\Illuminate\Support\Collection|\Illuminate\Database\Query\Builder */ protected function getScope(Request $request) { return User::query(); } }
The model
use \Illuminate\Database\Eloquent\Model; use \Engency\ModelValidation\Validatable; use Engency\DataStructures\CustomDataFormats; use Engency\DataStructures\ExportsCustomDataFormats; class User extends Model implements ExportsCustomDataFormats { use Validatable; // trait required for laravel-model-validation use CustomDataFormats; // trait required for eloquent-formatting protected $fillable = [ 'name', 'email' ]; /** * Make sure clients only receive data they are authorized for. * Visit complete documentation on custom export formats on; * https://github.com/Engency/eloquent-formatting */ protected $exports = [ 'default' => [ 'name', ], 'complete' => [ 'name', 'email' ] ]; /** * Basic validation for resource attributes. * Visit complete documentation on model validation on; * https://github.com/Engency/laravel-model-validation */ public function rules() : array { return [ 'name' => 'required|string', 'email' => 'required|email' ]; } }
Html response
The controller will find for the following views;
- resource-path/views/pages/resource-name-kebab-case/index.blade.php
- resource-path/views/pages/resource-name-kebab-case/create.blade.php
- resource-path/views/pages/resource-name-kebab-case/show.blade.php
- resource-path/views/pages/resource-name-kebab-case/edit.blade.php
Within the index.blade.php file, the $items variable will be present by default. The index.blade.php file could look as following;
<ul> @foreach($items as $item) <li>{{ $item->name }}</li> @endforeach </ul>
Any page showing a resource (show and edit) have access to the resource. The name of the variable is the name of the resource, in camel case. E.g., 'StreetSign' would be '$streetSign'. The show.blade.php file could look like this;
<p>You are viewing {{ $user->name }}.</p> <p>The corresponding email address is {{ $user->email }}.</p>
In addition to that, the controller uses specific error pages;
- resource-path/views/pages/error/unauthorized.blade.php
- resource-path/views/pages/error/notfound.blade.php
- resource-path/views/pages/error/conflict.blade.php
- resource-path/views/pages/error/forbidden.blade.php
- resource-path/views/pages/error/500.blade.php
JSON response
Response for the index call
{
"items": [
{"name": "John"},
{"name": "Doe"}
]
}
Contributors
- Frank Kuipers (GitHub)
- Feel free to contribute or submit feature-requests as issues.
License
This plugin is licenced under the MIT license.
engency/resource-controllers 适用场景与选型建议
engency/resource-controllers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.09k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「controller」 「laravel」 「resource」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 engency/resource-controllers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 engency/resource-controllers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 engency/resource-controllers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Give your JS App some Backbone with Models, Views, Collections, and Events.
Generates URLs for public Puli resources.
Add the resource system to symfony application
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Adds support for singular resource routes to the Laravel router.
Retrieve a WebResourceInterface instance over HTTP
统计信息
- 总下载量: 2.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-19