Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/razonyang/yii-resource-storage
Static Resource Storage for Yii framework
https://github.com/razonyang/yii-resource-storage
resource-storage yii yii2
Last synced: about 2 months ago
JSON representation
Static Resource Storage for Yii framework
- Host: GitHub
- URL: https://github.com/razonyang/yii-resource-storage
- Owner: razonyang
- License: bsd-3-clause
- Created: 2019-01-18T03:34:25.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-01-21T04:11:52.000Z (about 6 years ago)
- Last Synced: 2024-04-14T02:23:40.886Z (10 months ago)
- Topics: resource-storage, yii, yii2
- Language: PHP
- Size: 5.86 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Resource Storage Component for Yii Framework
This library aim to be an universal resource storage component that provides the most commonly used APIs to manager resources, such as:
**uploadFile**, **saveFile** and **deleteFile** etc.## Storages
- [LocalStorage](src/LocalStorage.php)
Feel free to add your storage here by PR.
> Any storage **MUST** implements [StorageInterface](src/StorageInterface.php).
## Install
```
$ composer require razonyang/yii-resource-storage
```## Usage
We take **LocalStorage** as example to explain how to use it.
```php
/** @var StorageInterface $storage */
$storage = Yii::$app->get('resourceStorage');
```### Config
```php
return [
'components' => [
'resourceStorage' => [
'class' => \RazonYang\Yii\ResourceStorage\LocalStorage::class,
'host' => 'http://localhost',
'path' => '@app/web',
],
],
];
```### Uploads File
```php
$src = __DIR__ . '/foo.jpg';
$dest = 'foo.jpg';
$url = $storage->uploadFile($src, $dest);
echo $url; // http://localhost/foo.jpg
```### Saves file
```php
$content = 'bar';
$filename = 'bar.txt';
$url = $storage->saveFile($content, $filename);
echo $url; // http://localhost/bar.txt
```### Deletes file
```php
$filename = 'foo.jpg';
try {
$deleted = $storage->deleteFile($deleteFile);
if (!$deleted) {
throw new \Exception('Failed to deleted file: ' . $filename);
}} catch (FileNotExistException $e) {
// Handles FileNotExistException
} catch (\Throwable $t) {
// Handles other error or exception.
}
```