princeferozepuria / http-response
通过预配置的方法发送带有自动状态码的HTTP JSON响应
v1.0.0
2022-08-20 06:25 UTC
README
Laravel Composer包,用于自动生成API响应,包括预配置的状态码处理方法。
安装
composer require princeferozepuria/http-response
导入HTTPResponse
use Prince\Ferozepuria\HTTPResponse;
扩展和使用
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Prince\Ferozepuria\HTTPResponse; use Illuminate\Support\Facades\Validator; //Feel Free To Visit https://navjotsinghprince.com class TestController extends HTTPResponse { /** * Example 1... */ public function example1(Request $request) { $collection = collect([1, 2, 3]); $class_Obj = new \stdClass(); $class_Obj->name="Prince Ferozepuria"; $response = [ "string" => "Prince Ferozepuria", "int" => 1, "boolean" => true, "array" => ["prince", "ferozepuria"], "collection" => $collection, "class_object" => $class_Obj, "is_null" => null, "is_empty" => "", ]; return $this->sendSuccess(SUCCESS,$response); } /** * Example 2... */ public function example2(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required', 'email'=>'required|email' ]); if ($validator->fails()) { return $this->validationFailed(ALL_FIELDS_REQUIRED, $validator->errors()); } } /** * Example 3... * Usage With Custom Object */ public function example3(Request $request) { $data = ["name" => "Prince Ferozepuria"]; $response = new HTTPResponse(); return $response->sendSuccess("This is just test message", $data); } }
可用方法
<?php $response = [ "name" => "Prince Ferozepuria", "email" => "fzr@navjotsinghprince.com", "website" => "https://navjotsinghprince.com" ]; return $this->sendSuccess("success response message", $response); return $this->sendSuccessForce("success force response message","total",$response); return $this->sendFailure("failed response message",$response); return $this->notFound("not Found response message",$response); return $this->validationFailed("validation failed response message",$response); return $this->forbidden("forbidden response message"); return $this->unauthorized("unauthorized response message"); return $this->dataProcessFailed("data process failed message");
技巧
💡 技巧:使用可用消息:SUCCESS , FAILED , ALL_FIELDS_REQUIRED , SOMETHING_WRONG , SOMETHING_WRONG_LATER
作者
参见参与此包的贡献者的网站。
联系
如果您在包中发现任何问题,请通过fzr@navjotsinghprince.com发送电子邮件给Prince Ferozepuria。您的问题都将得到解答。
变更日志
请参阅changelog.md了解最近的变化。
买我一杯咖啡!☕
随时可以在买我一杯咖啡!☕购买我一杯咖啡,无论是咖啡还是对我的工作的好评,对我来说都是极大的帮助。
捐赠
此包完全免费使用,但构建它花费了大量的时间。如果您想通过留下小额捐赠来表示感激,您可以点击此处此处。谢谢!
许可
本项目采用MIT许可协议 - 请参阅LICENSE.md文件以获取详细信息。