Yii2 extension that allows to work with files as with simple properties.
Usage of files like any other simple property.
Storages logic is separated and can be change without changing your controllers or views.
Formatters logic is separated. For each of file you can define formatters. This formatters allow to generate and use formatted versions of file. Added useful watermakr, thumb, grayscale and others formatters for images.
Useful getting url of source file or formatted version of file.
File storage allows to save origin file names.
Useful config for default file url (for dummy urls).
You can use ajax file uploader with minimal application config.
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist "flexibuild/file *"
or add
"flexibuild/file": "*"
to the require section of your composer.json.
Example of usage for avatar field in ContactForm model:
// ...
'contextManager' => [
'class' => 'flexibuild\file\ContextManager',
'contexts' => [
/* context name => [context config] */
'contact' => [
'class' => 'flexibuild\file\contexts\ImageContext',
//'defaultFormatter' => ['image/watermark', 'filename' => '@app/data/menus.png'], // example of default watermark
//'storage' => [
// 'class' => 'flexibuild\file\storages\FileSystemStorage', // default storage
//],
//'formatters' => [ // examples of formatters
// 'small' => ['image/thumb', 'width' => 200, 'height' => 200],
// 'mini' => ['from', 'from' => 'small', 'formatter' => ['image/thumb', 'width' => 100]],
// 'pixel' => ['from', 'from' => 'mini', 'formatter' => ['image/thumb', 'height' => 10]],
// 'temp1' => ['image/thumb', 'width' => 100],
//],
//'fileConfig' => [ // example of customizing File objects
// 'on cannotGetUrl' => 'flexibuild\file\events\CannotGetUrlHandlers::formatFileOnFly',
//],
],
],
// ...
// ...
public function behaviors()
{
return [
'fileModelBehavior' => [
'class' => \flexibuild\file\ModelBehavior::className(),
'attributes' => [
// add 'avatarFile' attribute that will be linked with 'avatar' property
// and use 'contact' context
'avatar' => 'contact',
],
],
];
}
// ...
After that your model has property 'avatarFile'. It type is \flexibuild\file\File object. For better compability with IDE we recommend to add this property in your model's PHPDoc:
/*
* ...
* @property \flexibuild\file\File $avatarFile
* ...
*/
public function rules()
{
return [
// ...
['avatarFile', 'file'/*, 'skipOnEmpty' => false */],
// ...
];
}
\flexibuild\file\widgets\SimpleFileInput or
\flexibuild\file\widgets\BlueimpJQueryUploader
widgets in your view file.
You can use ActiveFormEx or ActiveFieldEx (or FieldFileInputsTrait) for greater convenience. <?php
use flexibuild\file\widgets\bootstrap\ActiveFormEx;
// or flexibuild\file\widgets\ActiveFormEx without using bootstrap
?>
<!-- ... -->
<?php $form = ActiveFormEx::begin([/* ... */]); ?>
<!-- ... -->
<?= $form->field($model, 'avatarFile')->fileInput([
// custom widget options
]) ?>
<!-- ... -->
<?php ActiveFormEx::end(); ?>
// ...
'controllerMap' => [
'file' => [
'class' => 'flexibuild\file\web\UploadController',
'contexts' => [
'contact',
],
],
],
// ...
After that instead of SimpleFileInput you can use ajax jquery uploader:
<?= \flexibuild\file\widgets\BlueimpJQueryUploader::widget([
'model' => $model,
'attribute' => 'avatarFile',
'url' => ['file/upload-contact'],
]) ?>
OR
<?= $form->field($model, 'avatarFile')->fileBlueimpUploader([
'url' => ['file/upload-contact'],
]) ?>
<a href="<?= Html::encode($model->avatarFile) ?>"><?= Html::encode($model->name) ?></a>
<a href="<?= Html::encode($model->avatarFile->asMini) ?>"><?= Html::encode($model->name) ?></a>
<a href="<?= Html::encode($model->avatarFile->getUrl('small', 'https') ?>"><?= Html::encode($model->name) ?></a>
<p>size: <?= $model->avatarFile->size ?></p>
See \flexibuild\file\File object for more info.
Comments