spatie/laravel-morph-map-generator
Composer 安装命令:
composer require spatie/laravel-morph-map-generator
包简介
Automatically generate morph maps in your Laravel application
README 文档
README
With this package, you shouldn't worry about forgetting to add models to your application's morph map. Each model will autoregister itself in the morph map. The only thing you should do is implementing the getMorphClass method on your models like this:
class Post extends Model { public function getMorphClass(): string { return 'post'; } }
From now on, the Post model will be represented as post within your morph map.
Support us
We invest many resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-morph-map-generator
You can publish the config file with:
php artisan vendor:publish --provider="Spatie\LaravelMorphMapGenerator\MorphMapGeneratorServiceProvider" --tag="config"
This is the contents of the published config file:
return [ /* * When enabled, morph maps will be automatically generated when the * application is booted. */ 'autogenerate' => true, /** * Change the base directory if the models don't reside in the default App namespace. * * For example, the base directory would become 'src' if: * - Application is in src/App * - Models are in src/Domain */ 'base_directory' => '/', /* * Within these paths, the package will search for models to be included * in the generated morph map. */ 'paths' => [ app_path(), ], /* * Only models that extend from one of the base models defined here will * be included in the generated morph map. */ 'base_models' => [ Illuminate\Database\Eloquent\Model::class, ], /* * When generating the morph map, these models will not be included. */ 'ignored_models' => [], /* * Morph maps can be cached, there's a `FilesystemMorphMapCacheDriver` which * stores the morph map as a file in a directory or you can also use the * Laravel built-in cache by using `LaravelMorphMapCacheDriver`. * * Both drivers have their own config: * - `FilesystemMorphMapCacheDriver` requires a `path` to store the file * - `LaravelMorphMapCacheDriver` requires a `key` for storage */ 'cache' => [ 'type' => Spatie\LaravelMorphMapGenerator\Cache\FilesystemMorphMapCacheDriver::class, 'path' => storage_path('app/morph-map-generator'), ], ];
Usage
First, you have to implement getMorphClass for the models you want to include in your morph map. We suggest you create a new base model class in your application from which all your models extend. So you could throw an exception when getMorphClass was not yet implemented:
use Illuminate\Database\Eloquent\Model; abstract class BaseModel extends Model { public function getMorphClass() { throw new Exception('The model should implement `getMorphClass`'); } }
When a model is not implementing getMorphClass, it will throw an exception when building the generated morph map, making it possible to quickly find models that do not have a morph map entry.
When autogenerate is enabled in the morph-map-generator config file, the morph map in your application will be dynamically generated each time the application boots. This is great in development environments since each time your application boots, the morph map is regenerated. For performance reasons, you should cache the dynamically generated morph map by running the following command:
php artisan morph-map:cache
Removing a cached morph map can be done by running:
php artisan morph-map:clear
When using Laravel 11+, the morph map can also be cached by using Laravel's optimize commands: optimize and optimize:clear
Using a custom resolver
You can also determine morph class values programmatically by using a custom resolver. For example, you could use the following to automatically derive the value based on the singular form of the model's table name:
MorphMapGenerator::resolveUsing(fn ($model) => Str::singular($model->getTable()));
Be warned! When the output of the closure above is not stable then you'll manually need to update all the morhp_type columns within your database. Using something like the table name is a good idea since those do not change that often.
You may set the resolver in the boot method of one of your service providers.
Models outside your path
Some models like the default Laravel User model and models defined by packages will not be discovered by this package since it only searches for models within the app path and not the complete vendor directory. You can include these models in your morph map by using the default morph map feature from Laravel:
Relation::enforceMorphMap([ 'post' => 'App\Models\Post', 'video' => 'App\Models\Video', ]);
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.
spatie/laravel-morph-map-generator 适用场景与选型建议
spatie/laravel-morph-map-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 490.58k 次下载、GitHub Stars 达 75, 最近一次更新时间为 2020 年 10 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「laravel-morph-map-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-morph-map-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-morph-map-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-morph-map-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 490.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 75
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-29