Yii 2.0: yii2-file-behavior

Adds file behavior to Active Records
3 followers

File Behavior

Adds file behavior to Active Records

Features

  • Upload handeled throuth behaviors
  • Rewrite url with model variables
  • After Save update model for insert support
  • skipOnEmpty support

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist claudejanz/yii2-file-behavior "*"

or add

"claudejanz/yii2-file-behavior": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

In model

public function rules() {
        return array_merge(parent::rules(), [
            [['image'], 'file', 'extensions' => 'jpg'],
            [['image2'], 'file', 'extensions' => 'jpg'],
        ]);
    }
 
    public function behaviors() {
        return [
            'image' => [
                'class' => \claudejanz\fileBehavior\FileBehavior::className(),
                'paths' => '@webroot/images/all/{id}/',
            ],
        ];
    }

In Controller

public function actionCreate() {
        $model = new Vin;
 
        if ($model->loadWithFiles(Yii::$app->request->post()) && $model->save()) {
 
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }
 
    public function actionUpdate($id) {
        /* @var $model Vin */
        $model = $this->model;
        if ($model->loadWithFiles(Yii::$app->request->post()) && $model->save()) {
 
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

More about configuration in FileUploade comments

Futur developpment plan (when I need them)

  • Delete of old files
  • Multiple files upload for one field

Total 1 comment

#18415 report it
sintret at 2014/10/26 11:23am
how to crop behaviour too

nice a extension by the way its very nice if could to be crop image to thumbnail too.

thanks

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet