dominiquevienne/laravel-magic
Composer 安装命令:
composer require dominiquevienne/laravel-magic
包简介
Laravel helpers classes such as Controllers / Models / Requests
README 文档
README
Laravel Magic provides Abstract Controller, Model, generic Request, Traits, Exceptions and various middlewares in order to generate very easily and quickly API resources from scratch.
Support us
Laravel Magic is a free open source project. If you use the project, please star us on Github... it costs nothing but a click! 😉
Installation
You can install the package via composer:
composer require dominiquevienne/laravel-magic
This package does not provide
- any migration
- any configuration file
- any view
ENV variables to be set
# Duration of the cache handled by the package LARAVEL_MAGIC_CACHE_DEFAULT_DURATION=3600 # Used to ensure that queries handled by the AbstractController will use a Filters/ModelFilter.php class LARAVEL_MAGIC_FILTER_MODE=paranoid # Public key used to validate JWT tokens OAUTH_KEY_PUB=myPublicKey # Acceptable delay in seconds between two servers (used for tokens validation) JWT_LEEWAY=300
Usage
Models
Extending your models from AbstractModel will give you the opportunity to get the autocompletion that comes with PHPDoc in your IDE and will make it possible to check if a relationship exist for the given model.
It will also make it possible for AbstractController to make the magic happen. (see Controllers).
<?php namespace App\Models; use Dominiquevienne\LaravelMagic\Models\AbstractModel; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; /** * @property-read int $id * Your PHPDoc goes here */ class YourModel extends AbstractModel { use HasFactory; protected $fillable = [ // ... ]; }
Controllers
Extending your controllers from AbstractController will give you the opportunity to get a fully automated way to generate index, show, destroy, update, store methods without writing a single controller line. This will fill 90% of the API Resource needs.
Since default methods are already available in AbstractController, you can directly configure your routes to target the usual methods and magic will happen.
<?php namespace App\Http\Controllers; use Dominiquevienne\LaravelMagic\Http\Controllers\AbstractController; class YourController extends AbstractController { }
Relationships
When calling an index or show endpoint, you can add the with GET parameter in order to retrieve direct relationships of a given model.
The API will not throw an error / warning if relationship is not available.
If the property is not provided or if value is empty after sanitization, the query will result in a normal query without any relationship retrieval.
Fields
When calling an index or show endpoint, you can add the fields GET parameter in order to only retrieve the listed fields.
The API will not throw an error / warning if field is not available.
If the property is not provided or if value is empty after sanitization, the query will throw every available fields.
Filtering
When calling an index endpoint, you can add the filter GET parameter in order to filter the list so you only retrieve the row you targeted.
The API will not throw an error / warning if the fields you use for filtering are not available.
If the property is not provided or if value is empty after sanitization, the query will result in a normal query retrieving all the rows.
Ordering
When calling an index endpoint, you can add the filter GET parameter in order to order the provided result is ordered the way you need.
The API will throw an error if you provide a non-compliant string.
If the property is not provided, the query will result in a normal query retrieving all the rows without any specific sorting.
Pagination
The standard Laravel pagination is integrated. Please refer to the official paginate documentation.
Query Caching
By default Laravel Magic will cache queries used for Index and Show methods. It will use their fingerprint to ensure that the cache value is the one corresponding to a unique query. This caching method takes in consideration the in-app filtering, user-filtering, fields, ordering, relationships and pagination.
The default behaviour is to store the query result in cache for 8 hours but the value can be overridden in your .env file through the LARAVEL_MAGIC_CACHE_DEFAULT_DURATION variable. The value of this variable is TTL for cache in seconds. Use LARAVEL_MAGIC_CACHE_DEFAULT_DURATION=0 if you want to avoid totally query caching.
Requests
Laravel Magic provides a BootstrapRequest file which will be call on any AbstractConctroller.create and AbstractConctroller.update methods. If a request which name is ModelnameRequest is available in your Requests folder, it will be used to generate the validation rules.
Filtering queries
In your development, if you create a src/Http/Filters/ModelNameFilter.php class extending the GenericFilter class, Laravel Magic will automatically filter the way it retrieves data in your CRUD methods.
If this class has not been created, by default, no filtering is done. However, if you set LARAVEL_MAGIC_FILTERING_MODE=paranoid in your .env file, LaravelMagic will make it impossible to get any data.
Middlewares
Laravel Magic provides ForceJson and VerifyJwtToken middlewares. Those can be set up in Laravel Kernel.
Traits
We also provide the HasPublicationStatus, TimeStampsBy and SoftDeletesBy traits.
HasPublicationStatus trait
This trait will add a publication status to any of your model and store it in database.
To use the trait, add it to your models and migrate your schema so it has the publication_status_id field. Value of this field is handled by $publicationStatusAvailable property.
TimeStampsBy trait
This trait will add creatorand an updater relationships and will automatically store the user id in the created_by and updated_by table properties. Those properties have to be added through migration.
SoftDeletesBy trait
This trait will add deleter relationship and will automatically store the user id in the deleted_by table property. Those properties have to be added through migration.
Exceptions
Laravel Magic provides those Exceptions:
- ControllerAutomationException
- EnvException
- PublicationStatusException
- StatusUnknownException
These are used internally but you can of course use them as you want.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
dominiquevienne/laravel-magic 适用场景与选型建议
dominiquevienne/laravel-magic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.31k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel-magic」 「dominiquevienne」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dominiquevienne/laravel-magic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dominiquevienne/laravel-magic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dominiquevienne/laravel-magic 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 1.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-03