Skip to content

Commit a078525

Browse files
authored
Create WebmanReadme.md
1 parent 59e952d commit a078525

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

WebmanReadme.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<h1 style="text-align: center;"> support </h1>
2+
3+
## 安装
4+
5+
```shell
6+
$ composer require zhenmu/support -vvv
7+
```
8+
9+
## 基类创建控制器
10+
11+
```php
12+
./webman plugin:install zhen-mu/support
13+
14+
or
15+
16+
php ./vendor/zhenmu/support/src/scripts/install.php
17+
```
18+
19+
## 使用
20+
21+
### 控制器
22+
23+
1. 通过 `./webman make:controller` 控制器生成后,继承同目录下的 `WebmanBaseController` 基类。
24+
2. 编写接口时可通过 `$this->success($data = [], $err_code = 200, $messsage = 'success');` 返回正确数据给接口。
25+
3. 编写接口时可通过 `$this->fail($messsage = '', $err_code = 400);` 返回错误信息给接口。
26+
4.`support/exception/Handler.php``render` 函数中,调用 `WebmanResponseTrait``$this->renderableHandle($request, $exception);` 示例如下:
27+
28+
```php
29+
<?php
30+
31+
namespace support\exception;
32+
33+
use Webman\Http\Request;
34+
use Webman\Http\Response;
35+
use Throwable;
36+
use Webman\Exception\ExceptionHandler;
37+
use ZhenMu\Support\Traits\WebmanResponseTrait;
38+
39+
/**
40+
* Class Handler
41+
* @package support\exception
42+
*/
43+
class Handler extends ExceptionHandler
44+
{
45+
use WebmanResponseTrait; // 这里需要引入 WebmanResponseTrait
46+
47+
public $dontReport = [
48+
BusinessException::class,
49+
];
50+
51+
public function report(Throwable $exception)
52+
{
53+
parent::report($exception);
54+
}
55+
56+
public function render(Request $request, Throwable $exception): Response
57+
{
58+
return $this->renderableHandle($request, $exception); // 这里进行调用,做了一些错误捕捉
59+
}
60+
}
61+
```
62+
63+
**注意:头请求未声明此次请求需要返回 json 数据时,`$this->fail($message, $err_code)` 的错误码需要符合 http status_code 响应码**

0 commit comments

Comments
 (0)