yii2-ica-mediafile
Extension for attaching media files to models
Installation
- Install the mediafile extension using composer:
composer require icalab/yii2-ica-mediafile
- Run the provided migration:
php yii migrate --migrationPath=@icalab/mediafile/migrations
- Create a directory mediafiles under the web directory and make it writable
for the web server.
Usage
The relevant code files contain documentation at the top of the files. There
is also the Voorbeeld model and the VoorbeeldController that you can use to
see an example of how this module works.
In short:
- Create your model.
- Attach the ModelWithMediafileBehavior behavior to your model.
- Create a join table named yourmodel_mediafile containing a column parentid
and a column mediafileid.
- Attach the behavior ControllerWithMediafileBehavior to your controller.
Supply the class name of your model as a parameter (modelClass).
- Create an action actionUnassign($id, $mediafile) in your controller and
make it call the unassignMediafile($id, $mediafile) method that comes from
the ControllerWithMediafileBehavior behavior.
- In your update method, handle uploading of files by adding code like the
following:
$model->newFile = UploadedFile::getInstance($model, 'newFile');
if($model->validate() && (! $model->newFiles || $this->saveMediaFiles($model)))
{
$model->save();
$this->redirect(['update', 'id' => $id]);
}
- In your form view, show the output of the AttachMediafileWidget widget.
Supply the model and the form as a parameter to this widget:
echo AttachMediafileWidget::widget(['model' => $model, 'form' => $form]);
Notes
- Uploaded media files can be accessed from the web using the url
mediafile/view?id=id_of_file
- By default uploaded images are converted to PNG to avoid further loss of
quality. This was a requirement for the project we built this module for.
If you don't want this, pass an extra parameter FALSE to the saveMediafile
method the model inherits from the ModelWithMediafileBehavior behavior.
- The AttachMediafileWidget uses Kartik Visweswaran's excellent FileInput
widget: https://github.com/kartik-v/yii2-widget-fileinput
Comments