- 参数绑定
- 参数查找
- search 参数
- 绑定到模型
- 文件上传
参数绑定
参数查找
rapphp 的控制器的方法支持自动参数绑定
namespace app\index\controller;class UserController{public function save($id,$name,$sex){return ['success'=>true];}}
save方法里的 $id,$name,$sex可以自动由GET 参数获取或POST 参数获取,如果 PUT的是 json 也可以自动获取
search 参数
很多人喜欢这样的url /index/12/3434/test/34这里路径上有三个是数字路径查找等于 去掉数字后的路径 /index/test
三个参数可以通过下面方式获取
public function test(Request $request) {$search = $request -> search();//获取到search 参数$cat_id = $search[0];$test_id = $search[1];$id = $search[2];return ['success'=>true];}
当然也支持绑定
//Search的是按出现的顺序取值public function test(Search $cat_id, Search $test_id, Search $id) {$cat_id = $cat_id -> value();$test_id = $test_id -> value();$id = $id -> value();;return ['success'=>true];}
绑定到模型
自动绑定的参数支持绑定到对象的对应属性上
namespace app\index\model;class User extend extends Record{public $id;public $name;public $sex;}* * * * * * * * * * * * * * * * * * * * * * * * *namespace app\index\controller;class UserController{public function save(User $user){$user->save();return ['success'=>true];}}
这里绑定到的对象 是Record(模型)的子类;也可以绑定到任意模型上
文件上传
通过File对象可以绑定上传的同名文件
namespace app\index\controller;use rap\storage\File;class FileController{public function upload(File $file){return $file;}}
更多文件上传的内容需要查看Storage 模块的文档
上一篇:控制器前置方法 下一篇:依赖注入
