PHP code example of themismin / laravel-api
1. Go to this page and download the library: Download themismin/laravel-api 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/ */
themismin / laravel-api example snippets
/** response code common */
return [
/** 自定义码 => HTTP 响应状态码, 预留, 响应消息 */
/** 未认证 */
'401' => ['status_code' => 401, 'status' => 'error', 'message' => 'Unauthenticated.',],
/** 参数错误 */
'422' => ['status_code' => 422, 'status' => 'error', 'message' => 'The given data was invalid.',],
/** 成功 */
'200' => ['status_code' => 200, 'status' => 'success', 'message' => '成功',],
/** http_code 200, 接口 code 200401 */
'200401' => ['status_code' => 200, 'status' => 'success', 'message' => '成功',],
];
/** response code common */
return [
/** 自定义码 => HTTP 响应状态码, 预留, 响应消息 */
/** 未认证 */
'401' => ['http_code' => 401, 'status' => 'error', 'message' => 'Unauthenticated.',],
/** 参数错误 */
'422' => ['http_code' => 422, 'status' => 'error', 'message' => 'The given data was invalid.',],
/** 成功 */
'200' => ['http_code' => 200, 'status' => 'success', 'message' => '成功',],
/** 当 http_code 未设置时,默认为 200 */
'200' => ['status' => 'success', 'message' => '成功',],
/** http_code 200, 接口 code 200401 */
'200401' => ['http_code' => 200, 'status' => 'success', 'message' => '成功',],
/** 或 */
'200401' => ['status' => 'success', 'message' => '成功',],
];
// $app->singleton(
// Illuminate\Contracts\Debug\ExceptionHandler::class,
// App\Exceptions\Handler::class
// );
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
ThemisMin\LaravelApi\Exceptions\Handler::class
);
return response_json(['id' => "1"]);
> http_code:200 {"code":200,"status":"success","message":"success","data":{"id":"1"}}
return response_json(['id' => '1'], 401);
> http_code:401 {"code":401,"status":"error","message":"Unauthenticated.","data":{"id":"1"}}
return response_json(['id' => '1'], 200401);
> http_code:200 {"code":200401,"status":"error","message":"Unauthenticated.","data":{"id":"1"}}