yii2-extension yii2-attachments
Extension for file uploading and attaching to the models
dlds/yii2-attachments
Extension for file uploading and attaching to the models
- Tuesday, February 10, 2015
- by dlds
- Repository
- 1 Watchers
- 1 Stars
- 137 Installations
- PHP
- 0 Dependents
- 0 Suggesters
- 50 Forks
- 1 Open issues
- 3 Versions
- 1 % Grown
Yii2 attachments
Extension for file uploading and attaching to the models, (*1)
Installation
-
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require dlds/yii2-attachments "*"
or add, (*4)
"dlds/yii2-attachments": "*"
to the require section of your composer.json
file., (*5)
-
Apply migrations, (*6)
php yii migrate/up --migrationPath=@vendor/dlds/yii2-attachments/migrations
-
Add module to config/main.php
, (*7)
'modules' => [
...
'attachments' => [
'class' => dlds\attachments\Module::className(),
'tempPath' => '@app/uploads/temp',
'storePath' => '@app/uploads/store'
]
...
]
-
Attach behavior to your model (be sure that your model has "id" property), (*8)
public function behaviors()
{
return [
...
'attachmentBehavior' => [
'class' => \dlds\attachments\behaviors\AttachmentBehavior::className()
]
...
];
}
-
Make sure that you have added 'enctype' => 'multipart/form-data'
to the ActiveForm options, (*9)
Usage
-
In the form.php
of your model add file input, (*10)
<?= \kartik\file\FileInput::widget([
'name' => 'file[]',
'id' => 'file-input',
'options' => [
'multiple' => true, // false if you want to allow upload a single file
],
'pluginOptions' => [
'uploadUrl' => yii\helpers\Url::toRoute('/attachments/file/upload'), // remove this if you don't want to use AJAX uploading
'initialPreview' => $model->isNewRecord ? [] : $model->getInitialPreview(),
'initialPreviewConfig' => $model->isNewRecord ? [] : $model->getInitialPreviewConfig(),
// other options
]
]); ?>
-
Use widget to show all attachments of the model in the view.php
, (*11)
<?= \dlds\attachments\components\AttachmentsTable::widget(['model' => $model]) ?>
-
(Optional) Add onclick action to your submit button that uploads all files before submitting form, (*12)
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', [
'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary',
'onclick' => "$('#file-input').fileinput('upload');"
]) ?>
Change log
-
Feb 2, 2015 - Fix: all attached files will be deleted with the model.
-
Feb 1, 2015 - AJAX or basic upload.
-
Jan 30, 2015 - Several previews of images and other files, fix of required packages.
-
Jan 29, 2015 - First version with basic uploading and previews.
dev-master
9999999-dev
Extension for file uploading and attaching to the models
Sources
Download
BSD-4-Clause
The Requires
by
Jiri Svoboda
file
extension
yii2
upload
attachment
1.1
1.1.0.0
Extension for file uploading and attaching to the models
Sources
Download
BSD-4-Clause
The Requires
by
Jiri Svoboda
file
extension
yii2
upload
attachment
1.0
1.0.0.0
Extension for file uploading and attaching to the models
Sources
Download
BSD-4-Clause
The Requires
by
Jiri Svoboda
file
extension
yii2
upload
attachment