Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • ThinkPHP

  • FastAdmin

  • laravel

    • Laravel路由
    • Laravel ORM模型
    • Laravel请求
      • 常用方法
        • 获取请求路径
        • 检查请求路径
        • 检查请求路由
        • 获取请求URL
        • 获取/验证请求方法
        • 请求头
        • 请求Ip
        • 请求接受 Accept
      • 输入
        • 获取所有输入数据
        • 获取指定参数输入值
        • 从查询字符串中获取(GET)
        • JSON输入值
        • bool输入值
        • 提取一部分数据
        • 判断输入值是否存在
    • Laravel视图
    • Laravel Blade模板
    • Laravel Blade模板-组件
    • Laravel 响应
    • laravel常见问题
    • laravel数据迁移给表添加注释
    • laravel + dinggo + jwt搭建接口认证 用户认证
    • laravel中使用scss和tailwindcss
  • laravel-jetstream.vue

  • laravel常用扩展包

  • 《PHP框架》
  • laravel
mrcdh
2021-07-05
目录

Laravel请求

laravel8.*

依赖注入

use Illuminate\Http\Request;
1

facade

use Illuminate\Support\Facades\Request;
1

辅助函数

request();
1

# 常用方法

# 获取请求路径

$uri = $request->path();
// http://example.com/foo/bar,则 path 方法将返回 foo/bar
1
2

# 检查请求路径

if ($request->is('foo/*')) {
  //
}
1
2
3

# 检查请求路由

if ($request->routeIs('foo.*')) {
  //
}
1
2
3

# 获取请求URL

// 返回不带查询字符串(?=*)的url
$url = $request->url();

// 返回带查询字符串的url
$fullUrl = $request->fullUrl();
1
2
3
4
5

# 获取/验证请求方法

$method = $request->method();

if ($request->isMethod('post')) {
  //
}
1
2
3
4
5

# 请求头

$value = $request->header('X-Header-Name');

// 使用第二个参数设置不存在时的默认值
$value = $request->header('X-Header-Name', 'default');  

// 判断是否包含指定请求头
if ($request->hasHeader('X-Header-Name')) {
    //
}

// bearerToken 方法可用来从 Authorization 头中检索一个 bearer 令牌。如果这样的头不存在,一个空字符串将被返回
$token = $request->bearerToken();
1
2
3
4
5
6
7
8
9
10
11
12

# 请求Ip

$ip = $request->ip();
1

# 请求接受 Accept

使用 getAcceptableContentTypes 方法返回一个包含通过请求接受的所有内容类型的数组:

$contentTypes = request()->getAcceptableContentTypes();
// 返回 ["text\/html","application\/xhtml+xml","image\/webp","image\/apng","application\/xml","application\/signed-exchange","*\/*"]
1
2

accepts 方法接受内容类型的数组,如果请求接受任何内容类型,则返回 true。 否则,将返回 false:

if ($request->accepts(['text/html', 'application/json'])) {
  // ...
}
1
2
3

你可以使用 prefers 方法来确定给定内容类型数组中哪种内容类型最受请求青睐。如果请求不接受任何提供的内容类型,则将返回 null:

$preferred = $request->prefers(['text/html', 'application/json']);
1

由于许多应用程序仅提供 HTML 或 JSON,因此你可以使用 expectsJson 方法来快速确定传入的请求是否需要 JSON 响应:

if ($request->expectsJson()) {
    // ...
}
1
2
3

# 输入

# 获取所有输入数据

$input = $request->all();
1

# 获取指定参数输入值

$name = $request->input('name');

// 使用第二个参数来制定默认值
$name = $request->input('name', 'mrcdh');

// 当输入包含数组时可以使用(.)来访问
$name = $request->input('products.0.name');
$names = $request->input('products.*.name');

// 不传参数则获取所有输入
$input = $request->input();
1
2
3
4
5
6
7
8
9
10
11

# 从查询字符串中获取(GET)

$name = $request->query('name');

// 指定默认值
$name = $request->query('name', 'Helen');

// 将所有查询字符串作为关联数组
$query = $request->query();

// 使用get,必须指定参数名称
$name = $request->get('name');
1
2
3
4
5
6
7
8
9
10

# JSON输入值

向应用程序发送 JSON 请求时,只要将请求的 Content-Type 标头正确设置为 application/json,就可以通过 input 方法访问 JSON 数据。甚至可以使用 “点” 语法来检索嵌套在 JSON 数组中的值:

$name = $request->input('user.name');
1

# bool输入值

使用 boolean 方法将为 1,“1”,true,“true”,“on” 和 “yes” 返回 true。所有其他值将返回 false:

$archived = $request->boolean('is_open');
1

# 提取一部分数据

// 只需要
$input = $request->only(['username', 'password']);

$input = $request->only('username', 'password');

// 排除
$input = $request->except(['credit_card']);

$input = $request->except('credit_card');
1
2
3
4
5
6
7
8
9

注意

only 方法返回你请求的所有键 / 值对;但是,它不会返回请求中不存在的键 / 值对。

# 判断输入值是否存在

使用 has 方法判断给定的值是否存在,存在返回 true。

if ($request->has('name')) {
  //
}

if ($request->has(['name', 'email'])) {
  //
}
1
2
3
4
5
6
7

hasAny 将在指定的值中有一个存在返回 true。

if ($request->hasAny(['name', 'email'])) {
  //
}
1
2
3

whenHas 将在指定的值存在时执行闭包。

$request->whenHas('name', function ($input) {
  //
});
1
2
3

filled: 判断指定的值 存在 且不为 空 返回 true

if ($request->filled('name')) {
  //
}
1
2
3

whenFilled: 存在且不为空执行闭包

$request->whenFilled('name', function ($input) {
    //
});
1
2
3

missing: 值是否缺失,缺失返回true,正好同 has 方法相反

if ($request->missing('name')) {
  //
}
1
2
3
#php#laravel
上次更新: 2023/12/22, 10:33:26
Laravel ORM模型
Laravel视图

← Laravel ORM模型 Laravel视图→

最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×