years / yii2-storage
Resource (Cloud) Storage SDK for Yii2
1.0.4
2017-09-17 06:28 UTC
Requires
- php: >=5.3.3
- aliyuncs/oss-sdk-php: ~2.0
- league/flysystem: ^1.0
- qiniu/php-sdk: ^7.0
- xutl/yii2-collection: ~1.0.0
- yiisoft/yii2: ~2.0.5
This package is not auto-updated.
Last update: 2025-03-25 19:38:08 UTC
README
This package is one ActiveRecord behavior. It allows you to keep the uploaded file as-is. It support multiple attributes and files.
Based on Flysystem
, qiniu
and oss
supported.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist years/yii2-storage "*"
or add
"years/yii2-storage": "*"
to the require
section of your composer.json.
Configuration
Add component 'storage'
'components' => [
'storage' => [
'class' => 'years\storage\Storage',
],
]
Add key filesystems
to params.php
return [
xxxxx
'filesystems' => [
'default' => 'qiniu',
'disks' => [
'local' => [
'driver' => 'local',
'root' => '@webroot/upload',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
'qiniu' => [
'driver' => 'qiniu',
'domains' => [
'default' => 'olys54us5.bkt.clouddn.com', //你的七牛域名
'https' => '', //你的HTTPS域名
'custom' => '', //你的自定义域名
],
'access_key'=> 'xxxxx-BCT2', //AccessKey
'secret_key'=> 'xxxx', //SecretKey
'bucket' => 'xxx', //Bucket名字
'notify_url'=> '', //持久化处理回调地址
],
'oss' => [
'driver' => 'oss',
'access_id' => 'xxxx',
'access_key' => 'xxx',
'bucket' => 'xxx',
'endpoint' => 'oss-cn-hangzhou-internal.aliyuncs.com',
'isCName' => false,
'debug' => true
],
],
],
];
UploadBehavior
This behavior allow you to add file uploading logic with ActiveRecord behavior.
Usage
Attach the behavior to your model class:
public function behaviors()
{
return [
[
'class' => '\years\storage\behaviors\UploadBehavior',
'attributes' => 'avatar',
'disk' => 'qiniu',
'directory' => 'images',
],
];
}
Add validation rule:
public function rules()
{
return [
['avatar', 'file'],
];
}
Setup proper form enctype:
$form = \yii\bootstrap\ActiveForm::begin([
'enableClientValidation' => false,
'options' => [
'enctype' => 'multipart/form-data',
],
]);
File should be uploading fine.
Licence
MIT