# 修饰方法
此页面参照了EasySwoole Distributed的文档,原文档地址
[https://www.kancloud.cn/tmtbe/goswoole/1086146](https://www.kancloud.cn/tmtbe/goswoole/1086146)
## 通用注解修饰方法
修饰方法的作用是对路由进行一些预处理处理再返回给方法。
> 以下修饰方法不管是GET或者POST,均可使用
| 注解名称 | 注解作用 |
| :------------ | :----------------------------------------------------------- |
| @PathVariable | 获取自定义路由 `占位符` 的参数,并导入到方法中(支持多个),如果增加参数 `required=true`,则不到该参数会抛出一个http 400的 异常 |
| @RequestParam | 获取路由某个参数也就是`?`后面的,并导入到方法中(支持多个) |
| @ResponseBody | 对回的数据增加 Content-type :json,否则默认为html |
```php
/**
* post 表单 请求
* @PostMapping("test4/{name}")
* @PathVariable("name", required=true)
* @param $name
* @return string
*/
public function actionTest4($name){
var_dump($name);
return "test444";
}
```
访问 [http://serverName:8080/test/hello](http://127.0.0.1:8080/test/hello) 。那么`hello` 就会传递到name绑定的变量中去,最终test4(`$name`) 被传递了 hello。如果没有传递{name},则会抛出 http 400 的异常。
```php
/**
* get请求
* @GetMapping("test/{name2}/{name3}")
* @PathVariable("name2")
* @PathVariable("name3")
* @RequestParam("id")
* @param $name2
* @param $name2
* @param $id
* @return string
*/
public function actionTest($name2, $name3, $id)
{
var_dump($name2, $name3, $id);
return "test222";
}
```
访问 http://127.0.0.1:8080/test/hello/world?id=99 。那么`hello`会传递到 `$name2`变量,`world`会传递到`$name3`的变量中,`99`会传递到`$id`中。
> PathVariable,RequestParam 还支持 required 参数,如果设置为true,那么当该参数不存在时会抛出异常
# POST修饰方法
POST修饰方法帮助我们在接收到参数的时候进行一些预处理。
> 以下修饰方法仅限在POST,PUT下使用。
| 注解名称 | 注解作用 |
| :--------------- | :----------------------------------------------------------- |
| @RequestBody | 从原始请求raw中解析json字符串,并转为数组。如果解析失败抛出异常。 |
| @RequestRawJson | 同上 |
| @RequestRaw | 直接获取post的未编码raw数据 |
| @RequestRawXml | 从从原始请求raw中解析xml字符串,并转为数组。如果解析失败抛出异常。 |
| @ModelAttribute | 获获取POST数据,导入给model对象,或传给一个数组。 |
| @RequestFormData | 获获取POST数据的一个参数,传给一个参数。 |
> POST修饰注释,只能在申明了 PostMapping()注解的方法上使用
## @RequestBody
`POST`一个`raw`的 json 数据。`$body`收到的就为转化成数组的表单数据,如果解析失败抛出异常。
```php
/**
* @PostMapping()
* @RequestBody("body")
*/
public function actionTest8($body){
print_r($body);
return 'test8';
}
```
## @RequestRaw**
`POST`一个 raw数据,`$raw`收到的就为原始表单数据。
```php
/**
* @PostMapping("test6/{name}")
* @RequestRaw("raw")
* @param $raw
* @return string
*/
public function actionTest6($raw){
var_dump($name, $raw);
return "test6";
}
```
## @RequestRawXml**
`POST`一个xml raw,那么`$test`收到的就为转换成数组后的表单数据。
```php
/**
* @PostMapping()
* @RequestRawXml("test")
* @param $test
* @return string
*/
public function actionTest7($test){
var_dump($test);
return 'test7';
}
```
## @ModelAttribute**
> 如果使用 RequestBody,RequestRaw* 相关方法,则该注解不可用。
`POST`一个表单数据(form-data或www-form-urnecoded),testForm 类型的 $test 变量会被填充数据。
如果传递page=1,对象的属性就会被自动覆盖。如果不给变量传递类型,则该变量拿到post所有数据。
```php
/**
* post 表单 请求
* @PostMapping("test3/{name}")
* @PathVariable("name")
* @ModelAttribute("test")
* @param $name
* @param $test
* @return string
* 实际上是 获取整个post表单的数据 到声明的变量里
*/
public function actionTest3($name, testForm $test)
{
print_r($this->request->post());
var_dump($name, $test);
return "test444";
}
class testForm {
public $page = 1;
public $test = 0;
}
```
## RequestFormData
将表单的一个参数填充到类方法的变量中。
POST hhh=123,则 `$hhh` 为 123
```php
/**
* post 表单 请求
* @PostMapping("test4/{name}")
* @PathVariable("name")
* @RequestFormData("hhh")
* @param $name
* @param $test
* @return string
* 知己上只能获取声明的字段
*/
public function actionTest4($name, $hhh){
var_dump($name, $hhh);
return "test444";
}
```
- 1 介绍
- 2 安装
- 2.1 环境
- 2.2 安装
- 3 配置
- 3.1 Server配置
- 3.2 端口配置
- 3.3 项目结构
- 3.4 内核优化
- 4 服务
- 4.1 HTTP服务
- 4.1.1 路由
- 4.1.1.1 静态路由
- 4.1.1.2 路由定义
- 4.1.1.3 路由方法
- 4.1.1.4 路由分组
- 4.1.1.5 资源路由
- 4.1.1.6 端口作用域
- 4.1.1.7 异常处理
- 4.1.1.8 跨域请求
- 4.1.1.9 路由缓存
- 4.1.2 控制器
- 4.1.2.1 控制器初始化
- 4.1.2.2 前置后置操作
- 4.1.2.3 跳转与重定向
- 4.1.2.4 异常处理
- 4.1.3 请求
- 4.1.3.1 请求对象
- 4.1.3.2 请求信息
- 4.1.3.3 REQUEST消息
- 4.1.3.4 RESPONSE消息
- 4.1.3.5 STREAM消息
- 4.1.3.6 URI信息
- 4.1.3.7 处理上传文件
- 4.1.3.8 验证器
- 4.2 Websocket服务
- 4.2.1 Websocket配置
- 4.2.2 Websocket路由
- 4.3 TCP服务
- 4.3.1 TCP配置
- 4.3.2 TCP路由
- 4.3.3 协程处理案例
- 5 插件
- 5.15 Yii-PDO插件
- 5.15.1 PDO 连接MySQL Mariadb
- 5.15.2 PDO连接PostgreSQL
- 5.15.3 PDO连接GreenPlum
- 5.15.4 PDO连接Oracle
- 5.15.5 PDO连接Cubrid
- 5.15.6 PDO连接SQL Server
- 6 概念
- 7. Yii- I18N国际化
- 8. 模型 Yii-Model
- 8.1 快速创建模型
- 8.2 快速创建多个模型
- 8.3 核心验证器 Core Validators
- 9. 配合数据库工作
- 9.1.数据库访问对象 Database Access Objects
- 9.2 查询构造器 Query Builder
- 9.3 活动记录 Active Record
