Laravel 5.5 Request

Request(请求)

$path = $request->path();  // 获取请求路径
$request->is('user/*');  // 验证请求路径是否与给定模式匹配,该方法参数支持 * 通配符
// 获取请求URL
    $url = $request->url();  // 不包含查询字符串
    $url_with_query = $request->fullUrl();  // 包含查询字符串
// 获取请求方法
    $method = $request->method(); // GET/POST
    if($request->isMethod('post')){
        // true or false
    }
// 获取请求输入
    (获取所有输入值)
        $input = $request->all();
    
    (获取单个输入值)
        $name = $request->input('name');
        $name = $request->input('name', '学院君'); // 如果请求输入值在当前请求 URL 中未出现时该值将会被返回
        $input = $request->input('products.0.name'); // 处理表单数组输入时,可以使用”.”来访问数组输入
        $names = $request->input('products.*.name'); // 处理表单数组输入时,可以使用”.”来访问数组输入
    
    (从查询字符串中获取输入)
        $name = $request->query('name');
        $name = $request->query('name', '学院君');
        $query = $request->query();
    
    (通过动态属性获取输入)
        $name = $request->name;
    
    (获取JSON输入值)
        $name = $request->input('user.name');
    
    (获取输入的部分数据)
        $input = $request->only(['username', 'password']);
        $input = $request->only('username', 'password');
        $input = $request->except(['credit_card']);
        $input = $request->except('credit_card');
    
    (判断请求参数是否存在)
        if ($request->has('name')) {
            // 判断参数在请求中是否存在,可以使用 has 方法,如果参数存在则返回 true
        }
    
        if ($request->has(['name', 'email'])) {
            // 该方法支持以数组形式查询多个参数,这种情况下,只有当参数都存在时,才会返回 true
        }
    
        if ($request->filled('name')) {
            // 如果你想要判断参数存在且参数值不为空,可以使用 filled 方法
        }
// 上一次请求输入
    (将输入存储到Session)
        $request->flash();
        $request->flashOnly('username', 'email');
        $request->flashExcept('password');
    
    (将输入存储到 Session 然后重定向)
        如果你经常需要一次性存储输入请求输入并返回到表单填写页,可以在 redirect() 之后调用 withInput() 方法实现这样的功能
        return redirect('form')->withInput();
        return redirect('form')->withInput($request->except('password'));
    
    (取出上次请求数据)
        $username = $request->old('username');

// Cookie
    (从请求中取出Cookie)
        $value = $request->cookie('name');
    
    (添加Cookie到响应)
        return response('欢迎来到 Laravel 学院')->cookie(
            'name', '学院君', $minutes
        );
    
    (生成Cookie实例)
        如果你想要生成一个 Symfony\Component\HttpFoundation\Cookie 实例以便后续添加到响应实例,可以使用全局辅助函数 cookie,该 Cookie 只有在添加到响应实例上才会发送到客户端:
        $cookie = cookie('name', '学院君', $minutes);
        return response('欢迎来到 Laravel 学院')->cookie($cookie);

// 文件上传
    (获取上传的文件)
        $file = $request->file('photo');
        if ($request->hasFile('photo')) {
            // 可以使用 hasFile 方法判断文件在请求中是否存在
        }
    
    (验证文件是否上传成功)
        if ($request->file('photo')->isValid()){
            // 使用 isValid 方法判断文件在上传过程中是否出错
        }
    
    (文件路径 & 扩展名)
        $path = $request->photo->path();
        $extension = $request->photo->extension();
    
    (其他文件方法)
        https://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html
    
    (保存上传的文件)
        store();
            * 接收一个文件保存的相对路径(相对于文件系统配置的根目录 ),该路径不需要包含文件名,因为系统会自动生成一个唯一ID作为文件名, 还接收一个可选的参数 —— 用于存储文件的磁盘名称作为第二个参数(对应文件系统配置 disks 的键名,默认值是 local),该方法会返回相对于根目录的文件路径
                $path = $request->photo->store('images');
                $path = $request->photo->store('images', 's3');
    
            * 如果你不想自动生成文件名,可以使用 storeAs 方法,该方法接收保存路径、文件名和磁盘名作为参数:
                $path = $request->photo->storeAs('images', 'filename.jpg');
                $path = $request->photo->storeAs('images', 'filename.jpg', 's3');

猜你喜欢

转载自blog.csdn.net/qq_37910492/article/details/84543445