🌴 An upload component for Laravel.
-
Install package:
$ composer require overtrue/laravel-uploader -vvv
and publish the assets using command:
$ php artisan vendor:publish --provider=Overtrue\\LaravelUploader\\UploadServiceProvider
-
Routing
You can register routes in
routes/web.php
or other routes file:\LaravelUploader::routes(); // custom \LaravelUploader::routes([ 'as' => 'files.upload', 'middleware' => ['auth'], //... ]);
If you want to handle file upload, you can do it as simple as:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Overtrue\LaravelUploader\StrategyResolver;
class MyUploadController extends BaseController
{
public function upload(Request $request)
{
return StrategyResolver::resolveFromRequest($request, $request->get('strategy', 'default'))->upload();
}
}
If you want update the response, you can get key information from the return value object and return a new response:
public function upload(Request $request)
{
$response = StrategyResolver::resolveFromRequest($request, $request->get('strategy', 'default'))->upload();
return response()->json([
'status' => 'success',
'url' => $response->url,
'origin_name' => $response->originalName,
//...
]);
}
You can get all these public properties:
int $size;
string $path;
string $mime;
string $url;
string $relativeUrl;
string $filename;
string $originalName;
\Illuminate\Http\UploadedFile $file;
\Overtrue\LaravelUploader\Strategy $strategy;
如果你喜欢我的项目并想支持它,点击这里 ❤️
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
MIT