desmart / files
Files management for Laravel
Installs: 15
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 7
Forks: 0
Open Issues: 0
pkg:composer/desmart/files
Requires
- php: >=5.6.0
- desmart/support: 1.1.*
- illuminate/contracts: 5.3.*
- illuminate/database: 5.3.*
- symfony/http-foundation: 3.1.*
Requires (Dev)
- phpunit/phpunit: 5.6.*
README
This package handles files upload in friendly DDD manner.
Installation
In console run:
composer require desmart/files:2.0.*
Laravel
Add DeSmart\Files\ServiceProvider\ServiceProvider to providers list.
In console run:
php artisan vendor:publish php artisan migrate
Lumen
Add this line to bootstrap/app.php file:
$app->register('DeSmart\Files\ServiceProvider\LumenServiceProvider');
In console run:
cp vendor/desmart/files/database/migrations/* database/migrations/
cp vendor/desmart/files/config/desmart_files.php config/desmart_files.php
php artisan migrate
Configuration
Storage
This package uses Laravels storage mechanism. By default package uses upload disk which needs to be defined in config/filesystems.php.
<?php // config/filesystems.php return [ 'disks' => [ 'upload' => [ 'driver' => 'local', 'root' => public_path('upload'), ], ], ];
Mappers
Before saving file it can be mapped by a mapper. Mapper receives generated FileEntity and can change its properties. Based on entity data file will be saved in filesystem, and database.
Mappers must implement DeSmart\Files\Mapper\MapperInterface.
Custom File Entity class
By default package uses DeSmart\Files\Entity\FileEntity. This can be changed in desmart_files.file_entity_class config entry.
Examples
Storing file from upload
<?php $file = \Request::file('file'); $source = new \DeSmart\Files\FileSource\UploadedFileSource($file); // I'm assuming that Manager instance will be injected by Laravel Container $manager = app('DeSmart\Files\Manager'); // Here we have the FileEntity instance // File is saved on the filesystem and in the database $entity = $manager->store($source); // from here you have save the relation with other entity // this is just example! $user->addFile($entity); $user->save();
Removing file from storage
This method will work only when given FileEntity has no entries in file_records table.
<?php $file = new FileEntity; // $file should be obtained in different way (e.g through a relation) $manager = app('DeSmart\Files\Manager'); $manager->remove($file);