Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 3361

[extension] razonyang/yii2-uploader

$
0
0

Yii2 Uploader

  1. Installation
  2. Usage

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads LICENSE

Installation

composer require razonyang/yii2-uploader

Usage

Configuration:

return [
    'components' => [
        'uploader' => [
            'class' => \RazonYang\Yii2\Uploader\Uploader::class,
            'host' => 'http://localhost/resources', // the hostname relative to your uploaded files
            'filesystem' => [
                // filesystem 
                'class' => \creocoder\flysystem\LocalFilesystem::class,
                'path' => '@webroot/resources',
            ],
        ],
    ],
];

And then defines a form, UploadForm

class UploadForm extends \yii\base\Model
{
    use \RazonYang\Yii2\Uploader\UploadModelTrait;

    public function handle()
    {
        if (!$this->validate()) {
            // handles error
            throw new \Exception('invalid file');
        }

        $url = $this->upload();
        return [
            'url' => $url,
            // ... other information
        ];
    }
}

class UploadController extends \yii\web\Controller
{
    public function actionUpload()
    {
        $form = new UploadForm([
            'file' => \yii\web\UploadedFile::getInstanceByName('file')
        ]);
        return $form->handle();
    }
}

Viewing all articles
Browse latest Browse all 3361

Trending Articles