侧边栏壁纸
  • 累计撰写 71 篇文章
  • 累计创建 87 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录
PHP

【CoderPHP框架】11. Request请求

KunkkaWu
2020-07-22 / 0 评论 / 2 点赞 / 6,033 阅读 / 471 字 / 正在检测是否收录...

介绍

Request类用于请求相关的数据处理和验证。

使用

在控制器中,可以通过依赖注入的方式使用Request对象

<?php

namespace App\Http\Controllers;

use System\Request;
use System\Response;

class HomeController extends Controller {

    public function index(Request $request){
        $params = $request->all();
        return Response::json($params);
    }

}

获取参数

获取所有参数:all()

$request->all();

获取指定参数, 第二个参数表示未命中时返回的默认值,可选: input($param, $default)

$request->input('id');

判断是否包含参数: has($param)

$request->has('id');

获取url

获取URL路径:path(),结果为:/home

$request->path();

获取URL,:url(), 结果为: ‘http://getcoder.cn/home

$request->url();

获取完整URL,:fullUrl(), 结果为: ‘http://getcoder.cn/home?id=1

$request->rullUrl();

判断Method

获取当前请求的Method: method(), 结果为:GET/POST

$request->method();

判断当前请求Method: isMethod($method), 结果为:true/false

$request->isMethod('post');

判断当前请求Method是否为POST: isPost(), 结果为:true/false

$request->isPost();

判断当前请求Method是否为GET: isGet(), 结果为:true/false

$request->isGet();

文件上传

判断是否有指定文件上传:hasFile('image')

$request->hasFile('image')

获取文件对象:file('image')

$file = $request->file('image');

判断上传的文件是否正确:isValid()

$file->isValid()

从文件对象中获取临时文件路径:path()

$file->path()

从文件对象中获取文件扩展:extension(): 结果.jpg

$file->extension()

从文件对象中获取文件类型:getType():结果image/jpeg

$file->getType()

从文件对象中获取文件类型简写:getMimeType(): 结果 jpeg

$file->getMimeType()

从文件对象中获取文件大小:getClientSize(): 结果 110663 字节

$file->getClientSize()

从文件对象中获取原始文件名称:getClientOriginalName(): 结果 image.jpeg

$file->getClientOriginalName()
2

评论区