dcodegroup/laravel-attachments
Composer 安装命令:
composer require dcodegroup/laravel-attachments
包简介
Allow adding attachments to any document
关键字:
README 文档
README
Simple Dropin package to add attachments to your models.
Install
| Version / Branch | Laravel Support | Install Command |
|---|---|---|
| 0.x | <= v10 | composer require dcodegroup/laravel-attachments:^0.0 |
| 1.x | >= v11 | composer require dcodegroup/laravel-attachments:^1.0 |
Then run
php artisan vendor:publish --provider="Dcodegroup\LaravelAttachments\LaravelAttachmentsServiceProvider"
Database
If you are using either ULIDS or UUIDs in your tables ensure to update the published migrations for the media table.
eg. replace with the appropriate type
Schema::create('media', function (Blueprint $table) { ... $table->nullableMorphs('model'); $table->nullableMorphs('parent_model');
or
Schema::create('media', function (Blueprint $table) { ... $table->nullableUlidMorphs('model'); $table->nullableUlidMorphs('parent_model');
Then run the migrations
## Routes Add the Routes to the file you need such as `laravel_attachments.php` ```php <?php use Illuminate\Support\Facades\Route; Route::attachments(); Route::attachmentAnnotations(); Route::attachmentCategories();
Then add the following to your RouteServiceProvider
/** * Attachments */ Route::middleware([ 'web', 'auth', ])->as(config('attachments.route_name_prefix').'.')->prefix('attachments')->group(base_path('routes/laravel_attachments.php'));
Frontend
Add the following file to to your css file.
@import "../../vendor/dcodegroup/laravel-attachments/resources/scss/attachments.scss";
Add the below to the app.js file.
import attachmentPlugin from "../../vendor/dcodegroup/laravel-attachments/resources/js/plugin" app.use(attachmentPlugin)
Ensure to install these npm packages
npm i @heroicons/vue bytes form-backend-validation vue-image-markup vue-upload-component
config
Configuration file contains
<?php return [ 'media' => [ 'conversions' => [ 'thumb' => [ 'width' => 43, 'height' => 43, ], 'list' => [ 'width' => 43, 'height' => 43, ], 'grid' => [ 'width' => 160, 'height' => 192, ], ], ], 'features' => [ // annotations // categories ], 'route_name_prefix' => env('LARAVEL_ATTACHMENTS_ROUTE_NAME_PREFIX', 'laravel_attachments'), 'signed' => env('LARAVEL_ATTACHMENTS_URLS_SIGNED', false), ];
Ensure the publish the config file from the Spatie Media Library.
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-config"
Then change the media model used to
'media_model' => \Dcodegroup\LaravelAttachments\Models\Media::class,
Update the path generator from the default to our customised one.
'path_generator' => \Dcodegroup\LaravelAttachments\MediaLibrary\MediaPathGenerator::class,
You probably want to change the disk for stored files from media-library also.
'disk_name' => env('MEDIA_DISK', 's3'),
Security
You will need to implement a MediaPolicy to control access to the media.
Then add to AuthServiceProvider
protected $policies = [ ... Media::class => MediaPolicy::class, ];
Example policy might be
<?php namespace App\Policies; use App\Models\User; use Dcodegroup\LaravelAttachments\Models\Media; use Illuminate\Auth\Access\HandlesAuthorization; class MediaPolicy { use HandlesAuthorization; public function viewAny(User $user): bool { return $this->internalOnly($user); } public function view(User $user, Media $media): bool { return $this->internalOnly($user); } public function create(User $user): bool { return $this->internalOnly($user); } public function update(User $user, Media $media): bool { return $this->internalOnly($user); } public function delete(User $user, Media $media): bool { return $this->internalOnly($user); } public function restore(User $user, Media $media): bool { return $this->internalOnly($user); } public function forceDelete(User $user, Media $media): bool { return $this->internalOnly($user); } public function download(User $user): bool { return $this->internalOnly($user); } }
If you are using S3 or another hosted service you may need to use signed urls to access the urls.
If yes then update the configuration or the ENV variable to true.
'use_signed_urls' => env('USE_SIGNED_URLS', true),
This package will use dreamonkey/laravel-cloudfront-url-signer https://github.com/dreamonkey/laravel-cloudfront-url-signer. See the README for how to configure.
Here is how to generate the ssh keys. Make sure to have a directory storage/cloudfront-keypairs
Source of below is from https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html?icmpid=docs_cf_help_panel#private-content-creating-cloudfront-key-pairs
then
openssl genrsa -out private_key.pem 2048
then
openssl rsa -pubout -in private_key.pem -out public_key.pem
User model
Add the following contract to the User model.
use Dcodegroup\LaravelAttachments\Contracts\HasMediaUser; class User extends Authenticatable implements HasMediaUser { ... public function getMediaUserName(): string { return $this->name; }
Usage
Add the template to the edit page you want.
@if($model->exists) <div class="py-8"> <hr class="border-gray-100"> </div> <div class="mt-8"> @include('attachments::attachments', ['model' => $model]) </div> @endif
dcodegroup/laravel-attachments 适用场景与选型建议
dcodegroup/laravel-attachments 是一款 基于 Vue 开发的 Composer 扩展包,目前已累计 31.53k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「attachments」 「dcode」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dcodegroup/laravel-attachments 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dcodegroup/laravel-attachments 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dcodegroup/laravel-attachments 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple package which dcode uses to handle the connection to xero for all projects. Provides endpoints for UI and callbacks for Xero
Restore media attachments using WP CLI
DCODE FrontEnd preset for Laravel.
Simple package which dcode uses to handle application configuration and setting storage from third parties
File attachments for laravel.
Simple package which dcode uses to handle the connection to MYOB for all projects. Provides endpoints for UI and callbacks for MYOB
统计信息
- 总下载量: 31.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-09