aminkt/yii2-upload-manager

此包的最新版本(1.1.0)没有提供许可证信息。

基于并受WordPress上传管理器启发的上传管理器系统。

安装: 396

依赖: 1

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

1.1.0 2018-07-29 13:21 UTC

This package is auto-updated.

Last update: 2024-09-22 15:50:47 UTC


README

Yii2 上传管理模块。

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock

要安装此模块,请将以下内容添加到您的项目中的composer.json文件:

然后,将以下行添加到您的应用程序配置中

'upload-manager' => [
    'class' => aminkt\uploadManager\UploadManager::class,
    'uploadPath'=>Yii::getAlias("@frontendWeb")."/upload",
    'uploadUrl'=> "/upload",
    'baseUrl' => 'https://:800',
    'acceptedFiles'=>"*",
    'userClass' => 'user/class/namespcae',
    'fileClass' => 'file/class/namespcae', // Don't set this to use default active record.
    'fileSearchClass' => 'file/search/class/namespcae', // Don't set this to use default search active record.
],

然后运行以下代码以迁移模块

如果您正在使用默认模块模型,则运行迁移

php yii migrate --migrationPath="@vendor/aminkt/yii2-upload-manager"

如果您想编辑您的文件,请打开浏览器中的['uploadManager/default/index']

如何使用上传管理器小部件?

将以下行添加到您的视图文件中,以加载上传管理器小部件

<?php echo \aminkt\uploadManager\components\UploadManager::widget([
    'id'=>'upload-user-pic-'.$model->id,
    'model'=>$model,
    'attribute'=>'picture',
    'titleTxt'=>'تصویر ربات را  کنید.',
    'helpBlockEnable'=>false,
    'showImageContainer'=>'#avatar-'.$model->id,
    'showImagesTemplate'=>"<img src='{url}' class='img-responsive'>",
    'btnTxt'=>'<i class="fa fa-edit"></i>'
]);
echo Html::hiddenInput('target', $model->id, ['class'=>'target']);
?>
<div id="avatar-<?= $model->id ?>"></div>

使用上传管理器API上传和检索文件

首先将以下路由添加到您的API URL管理器中

[
    'class' => 'yii\rest\UrlRule',
    'pluralize' => false,
    'controller' => ['v2/upload' => 'uploadManager/v1/upload'],
    'extraPatterns' => [
        'GET load/<id:\d+>' => 'load',
    ]
],

上述代码表示使用v2/upload路由的uploadManager API版本1。以下路由现在可用

GET /v2/upload      // List of all files that user uploaded.
GET /v2/upload/[id]  // Detail of one single file.
GET /v2/upload/load/[id]  // Return file content of one single file by id
DELETE /v2/upload/[id]  // Delete a single file from server.
POST /v2/upload     // Upload a new file.

注意

  1. 除了加载和查看之外的所有请求至少应使用一种授权方法来授权用户。
  2. 在上传路由中,不要在标题中设置内容类型,而在其他路由中可以。
  3. 在上传路由中,以POST方式发送文件,文件为多部分类型,文件名最好为'file'。

警告:默认启用跨域。如果您遇到任何问题,请报告。

高级配置

如果您想实现自己的模型或搜索模型或想使用MongoDB数据库,请按照以下说明操作。

  1. 创建您的模型。
  2. 实现\aminkt\uploadManager\interfaces\FileInterface
  3. 如果您想使用默认的File常量,实现\aminkt\uploadManager\interfaces\FileConstantsInterface或创建自己的常量,名称与定义的接口类似。
  4. 根据第一部分所述配置您的模块,并定义fileClassfileSearchClass
  5. 您可以使用\aminkt\uploadManager\traits\FileTrait来实现定义在FileInterface中的一些常规方法

如果您想使用MongoDB活动记录,只需将配置部分中描述的fileClassfileSearchClass更改为使用MongoDB版本,例如\aminkt\uploadManager\models\mongo\File\aminkt\uploadManager\models\mongo\FileSearch