1. Go to this page and download the library: Download mitoop/laravel-api-response library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mitoop / laravel-api-response example snippets
use Mitoop\Http\RespondsWithJson;
class Controller extends BaseController
{
use RespondsWithJson;
}
class Controller extends BaseController
{
use RespondsWithJson;
public function successEmpty()
{
return $this->success();
}
public function successWithData()
{
return $this->success(['Hello']);
}
public function successWithPagination()
{
return $this->success(User::active()->paginate());
}
public function errorDefault()
{
return $this->error();
}
public function errorCustomMessage()
{
return $this->error('自定义错误信息');
}
public function unauthorized()
{
return $this->deny('登录信息已失效, 请重新登陆!');
}
}
use Illuminate\Http\Request;
use Mitoop\Http\Resources\Resource;
class LoraResource extends Resource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
}
use Mitoop\Http\Resources\ResourceCollection;
class LoraCollection extends ResourceCollection
{
}
class Controller extends BaseController
{
public function show()
{
// 直接返回标准化的 API Resource
return new LoraResource(Lora::find(1));
}
}
use Illuminate\Contracts\Debug\ExceptionHandler;
use Mitoop\Http\Exceptions\Handler;
public $singletons = [
ExceptionHandler::class => Handler::class,
];
use Illuminate\Foundation\Configuration\Exceptions;
->withExceptions(function (Exceptions $exceptions) {
// 只需要根据需求配置异常映射
// 其他异常由统一 Handler 接管,无需单独处理
$exceptions->map(JWTException::class, fn ($e) => new AuthenticationException);
})
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.