dcodegroup / laravel-attachments
允许向任何文档添加附件
1.0.0
2024-08-14 21:59 UTC
Requires
- php: ^8.2 || ^8.3
- ext-imagick: *
- dcodegroup/laravel-cloudfront-url-signer: ^3.6
- laravel/framework: ^11.0
- mvanduijker/laravel-model-exists-rule: ^3.0
- spatie/laravel-medialibrary: ^11.0
- spatie/pdf-to-image: ^2.2
Requires (Dev)
- ergebnis/composer-normalize: ^2.42
- larastan/larastan: ^2.9
- laravel/pint: ^1.13
- orchestra/testbench: ^9.0
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
This package is auto-updated.
Last update: 2024-09-14 22:22:41 UTC
README
一个简单的插件,用于向您的模型添加附件。
安装
然后运行
php artisan vendor:publish --provider="Dcodegroup\LaravelAttachments\LaravelAttachmentsServiceProvider"
数据库
如果您在表中使用 ULIDS
或 UUIDs
,请确保更新 media
表的已发布迁移。
例如,替换为适当的类型
Schema::create('media', function (Blueprint $table) { ... $table->nullableMorphs('model'); $table->nullableMorphs('parent_model');
或者
Schema::create('media', function (Blueprint $table) { ... $table->nullableUlidMorphs('model'); $table->nullableUlidMorphs('parent_model');
然后运行迁移
## 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();
然后向您的 RouteServiceProvider
添加以下内容
/** * Attachments */ Route::middleware([ 'web', 'auth', ])->as(config('attachments.route_name_prefix').'.')->prefix('attachments')->group(base_path('routes/laravel_attachments.php'));
前端
将以下文件添加到您的 CSS 文件中。
@import "../../vendor/dcodegroup/laravel-attachments/resources/scss/attachments.scss";
将以下内容添加到 app.js
文件中。
import attachmentPlugin from "../../vendor/dcodegroup/laravel-attachments/resources/js/plugin" app.use(attachmentPlugin)
请确保安装以下 npm 包
npm i @heroicons/vue bytes form-backend-validation vue-image-markup vue-upload-component
配置
配置文件包含
<?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), ];
确保发布来自 Spatie Media Library 的配置文件。
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-config"
然后更改使用的媒体模型
'media_model' => \Dcodegroup\LaravelAttachments\Models\Media::class,
更新路径生成器,从默认的更改为自定义的。
'path_generator' => \Dcodegroup\LaravelAttachments\MediaLibrary\MediaPathGenerator::class,
您可能还想将存储文件使用的磁盘从 media-library
更改为其他磁盘。
'disk_name' => env('MEDIA_DISK', 's3'),
安全
您需要实现一个 MediaPolicy
来控制对媒体的访问。
然后将其添加到 AuthServiceProvider
protected $policies = [ ... Media::class => MediaPolicy::class, ];
示例策略可能如下所示
<?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); } }
如果您使用 S3
或其他托管服务,您可能需要使用签名 URL 来访问 URL。
如果是,请更新配置或环境变量为 true。
'use_signed_urls' => env('USE_SIGNED_URLS', true),
此包将使用 dreamonkey/laravel-cloudfront-url-signer
https://github.com/dreamonkey/laravel-cloudfront-url-signer。请参阅 README 了解如何配置。
以下是生成 SSH 密钥的方法。请确保有 storage/cloudfront-keypairs
目录
然后
openssl genrsa -out private_key.pem 2048
然后
openssl rsa -pubout -in private_key.pem -out public_key.pem
用户模型
将以下合同添加到 User
模型中。
use Dcodegroup\LaravelAttachments\Contracts\HasMediaUser; class User extends Authenticatable implements HasMediaUser { ... public function getMediaUserName(): string { return $this->name; }
用法
将模板添加到您想编辑的页面。
@if($model->exists) <div class="py-8"> <hr class="border-gray-100"> </div> <div class="mt-8"> @include('attachments::attachments', ['model' => $model]) </div> @endif