Several components come with the package to reduce the effort of implementing a proper file-upload., (*6)
File-input
You can publish a file-input.vue component into your /resources/assets/js/components folder. Modify the component so it would fit into your design., (*7)
The vue-component uploads the file to a specific endpoint., (*8)
Note: You can avoid VueJS at all if you want so., (*9)
Endpoint(Route+Controller)
The package provides an Http endpoint for uploading files. You can disable the endpoint int the configuration file.
If you would like to define your custom endpoint, you can still reuse UploadControllerTrait., (*10)
Method | URI | Name | Action
POST | upload | upload | Zotyo\uFiler\Http\UploadController@upload
Eloquent
The HasFile trait provides methods so you can easily create accessors/mutators in you model.
In the following example User's avatar is a file., (*11)
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zotyo\uFiler\HasFile;
class User extends Authenticatable
{
use HasFile;
protected $fillable = ['avatar'];
protected $appends = ['avatar'];
public function getAvatarAttribute()
{
return $this->getFile('avatar');
}
public function setAvatarAttribute($value)
{
$this->setFile('avatar', $value);
}
}
Validation
Don't forget to validate files when submitting your entities. The package provides the verify-file-by-token validation rule to avoid hijacking of files., (*12)
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserFormRequest extends FormRequest
{
public function rules()
{
return [
'avatar' => 'required|verify-file-by-token'
];
}
}