先来看一下thinkphp5中的MVC
C 控制器 (Controller)
M 数据模型 (Model)
V 视图 (View)
控制器是负责协调Model和View之间的工作,控制器的方法中只能返回字符串或者response对象,一般我们返回的是模板,其实就是response对象
通过控制器获取请求参数,可以使用原生的 $_GET/$_POST,
继承Controller,可以调用Controller的request对象参数,可以通过get/post等方法获取参数,
系统的Controller:
1 2 3 4 5 6 7 8 9 |
/thinkphp/library/think/Controller.php class Index extends Controller{ public function index(){ $get = $this->request->get(); $post = $this->request->post(); $this->json($get); } } |
使用系统请求的好处:比如参数过滤,在config.php中添加配置
1 |
default_filter => 'htmlspecialchars()' |
就可以将特殊的符号转义成html编码
做个简单的文件上传,这个是上传的表单
1 2 3 4 |
<form action="doFile" method="post" enctype="multipart/form-data"> <input type="file" name="myFile"/> <input type="submit" value="submit"> </form> |
来看一下控制器里的写法,也很简单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Index extends Controller{ public function file(){ return $this->fetch(); } public function doFile(){ // dump($_FILES); $file = $this->request->file("myFile"); //获取上传的文件对象 $new_file = $file->move("./static/upload"); //直接使用系统提供的move方法,可以将上传的文件移动到自定义目录,返回一个新的文件对象 dump($file); dump($new_file); } } |
转载请注明:怼码人生 » thinkphp5 控制器