mlk9 / dual-response
使用此包,您可以在单个控制器中操作您的API和Web。 (Laravel 6≥)
v1.0.3
2022-08-02 17:03 UTC
Requires
- php: >=8.0.0
- illuminate/support: ~6|~7|~8|~9
README
双响应
使用此包,您可以在单个控制器中操作您的API和Web。 (Laravel 6≥)
使用此包,当您在API或Web的根路径发送请求时,可以接收不同的响应。
通过composer安装
$ composer require mlk9/dual-response
然后发布供应商
$ php artisan vendor:publish --tag=dual-response
文档
使用示例
response($webRoute //你的响应, $apiRoute //你的JSON响应)
BookController.php
use Mlk9\DualResponse\Facades\DualRes; ... /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $books = Book::paginate(40); return DualRes::response(view('book.index',compact('books')),['data' => $books]); }
您可以通过传递数组来更改默认值
默认API响应
'status_result' => true, 'status_code' => 200, 'message' => __('dualres.request_successful'), 'errors' => null, //removes when don't have any errors 'data' => null, //removes when don't have any data 'current_time' => now()->timestamp,
当您传递到键 error
时,默认响应
在API路由中
'status_result' => false, 'status_code' => 400, 'message' => __('dualres.request_not_valid'), 'errors' => [//your errors], 'current_time' => now()->timestamp,
在Web路由中返回您的响应。
当您传递到键 data
时,默认响应
在API路由中
'status_result' => true, 'status_code' => 200, 'message' => __('dualres.request_successful'), 'data' => [//your data], 'current_time' => now()->timestamp,
在Web路由中返回您的响应。
当您将 data
键传递为null时(未找到 - 404)的默认响应
在API路由中
'status_result' => false, 'status_code' => 404, 'message' => __('dualres.not_found'), 'current_time' => now()->timestamp,
在Web路由中中止404错误。