Laravel—请求
请求
Http中客户端向服务器发送的数据请求
依赖注入
即将HTTP的相关内容参数自动交给Illuminate\Http\Request类,可以用于控制器和路由参数  
在控制器中获取当前HTTP请求实例,需要在控制器中对Illuminate\Http\Request类进行依赖注入,这样当前的请求实例会被服务容器自动注入
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostControlle extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,$id)//依赖注入的形式
    {
        //如果控制器需要从路由参数中获取数据,也可以在依赖注入后面列出路由参数
    }
}
也可以在路由中使用request类,程序执行的时候,服务容器会自动的将请求注入到路由中,即通过路由访问请求
Route::get('/',function(\Illuminate\Http\Request $request){
    //
});
基本信息获取
获取请求方法
method方法会返回HTTP请求方式,还可以使用isMethod方法验证HTTP请求方式
$method = $request->method();//GET/POST
if($requset->isMethod('post')){
    //
}
获取请求路径
path方法将会返回请求的路径信息,如果请求的URL为http:localhost/user/1,则会返回user/1  
$path = $request->path();
is方法用于验证请求路径是否与给定模式匹配,可以使用*通配符
if($request->is('user/*')){
    //
}
获取请求的URL
用于获取完整的URL地址,而不只是路径信息,url方法返回不带查询字符串的URL,fulUrl方法返回包含查询字符串的结果
//对与返回 http://localhost/user/1?token=laravelacdemy.org
//返回 http://localhost/user/1
$url = $request->url();
//返回 http://localhost/user/1?token=laravelacdemy.org
$uel_with_query = $request->fullUrl();
获取ip和端口
ip和getPort分别用于获取ip地址和端口号  
提取请求参数
获取所有输入值
all方法以数组格式获取所有输入值,当请求的URL是http://localhost/user/1?name=xiaoming&age=8时:
$input = $request->all();
//input的返回值为:
array:2[
    "name" => "xiaoming"
    "age" => 8
]
获取单个输入值
使用input方法获取指定的字符串的值,把要获取的字符串传给input作为参数
还可以传递第二个参数作为默认值,当请求输入值在当前请求的URL中未出现时将返回该值
$name = $request->input(‘name’,’xiaoming’);
处理表单数组输入时,可以使用.来访问数组输入:
$input = $request->input('products.0.name');
$names = $request->input('products.*.name');
//当访问http://localhost/user?products[][name]=xioaming&products[][name]=damin 时,
$input = "xiaoming";
$names = array:2[
    0 => "xiaoming"
    1 => "daming"
];
从查询字符串中获取输入
input方法会从整个请求中获取数值,query只会从查询字符串从获取数值,同样query也可以使用第二个参数作为默认值
$name = $request->query(‘name’,’xiaoming’);
调用一个不带任何参数的query方法则会以关联数组的方式获取整个查询字符串的值
$query = $request->query();
- 此外还有一个
post方法,input、query、post三者的不同在于input用于获取所有HTTP请求,query用于获取GET请求,post用于获取post请求 
通过动态属性获取输入
如果应用表单上包含相应的字段(比如name字段),可以直接访问提交字段名提交的值:
$name = $request->name;
- 使用动态属性时,Laravel首先会在请求中查找参数的值,如果不存在,还会在路由参数中查找,使用魔术方法
__get() 
获取JSON输入值
发送JSON请求到应用时,只要Content-Type请求头被设置application/json,都可以通过input方法获取JSON数据,还可以通过.号解析数组
$name = $request->input(‘user.name’);
获取输入的部分数据
except和only方法,用于取出输入数据的子集, only方法如果想要获取的参数不存在,则对应的参数会被过滤掉
$input = $request->only(['username','password']);//只要这些数据
$input = $request->except(['username']);//除此之外的的数据
判断参数是否存在
使用has方法判断参数是否存在
if($request->has('name')){
    //
}
//当参数为数组时,只有都存在才会返回true
if($request->has(['name','email'])){
    //
}
还可以使用filled方法判断参数存在且值不为空  
获取头信息
header函数能够获取头信息
$header = $request->header(‘Connection’);
闪存(上一次请求输入)
Larevel可以保存上一次输入的数据(只存储一次)
将输入存储到Session
flash方法会将当前输入存放到一次性Session中,只能一次性使用
$request->flash();
flashOnly和flashExcept方法可以收集数据子集存放到Session中
$request->flashOnly('username','email');
$request->flashExcept('password');
将输入存入到Session然后重定向
用于表单自动填入上一次的数据,使用redirect之后调用withInput方法
return redirect('form')->withInput();
return redirect('form')->withInput($request->except('password'));
取出上次请求数据
要从Session中取出上次请求的输入数据,可以使用old方法,可以从Session中取出一次性数据
$username = $request->old(‘username’);
Laravel提供一个全局的辅助函数old(),可以在HTML代码中直接调用,如果给定参数没用对应输入,返回null
<input type="text" name="username" value="{{old('username')}}">
Cookie操作
读取Cookie
使用cookie方法从请求中获取Cookie的值,
- Larevel框架创建的所有Cookie都经过加密并使用一个认证码进行签名
 
两种使用方法:
$value = $request->cookie(‘name’);
$value = Cookie::get(‘name’);
添加Cookie到响应
使用cookie方法将一个Cookie添加到,需要传递的值有Cookie名称、值、有效期(分钟)response的参数值作为为响应的返回值,可以直接显示出来
两种使用方法:
return response('Laravel')->cookie(
    'name','xiaoming',$minutes
);
//queue()方法接受一个Cookie实例或创建一个Cookie实例
Cookie::queue(Cookie::make('name','value',$minutes));
Cookie::queue('name','value',$minutes);
生成Cookie实例
全局辅助函数cookie可以生成一个Cookie以便后续添加到响应实例
$cookie = cookie('name','value',$minutes);
return response()->cookie($cookie);
文件上传
获取文件上传
file方法或者动态属性可以访问上传文件
$file = $request->file(‘photo’);
$file = $request->photo;
hasfile方法判断文件在请求中是否存在:
if($request->hasfile('photo')){
    //
}
验证文件是否上传成功
isValid方法判断文件在上传过程中是否出错:
if ($request->file('photo')->isValid()){
    //
}
文件路径 & 拓展名
extension方法可以基于文件内容判断文件拓展名  
$path = $request->photo->path();
$extension = $request->photo->extension();
保存上传的文件
文件系统的配置位于config/filesystems.php,Laravel默认使用local配置存放上传文件,默认根目录时storage/app,public中的文件可以被公开访问store方法可以将上传文件移动到相应的磁盘路径上,接收一个文件保存的相对路径(相对于文件系统配置的根目录),不需要包含文件名,系统自动生成唯一ID作为文件名  
$path = $request->photo->store(‘images’);
storeAs可以自定义生成的文件名
$path = $reques->photo->storeAs(‘images’,’filename.jpg’);
要使上传的文件可以在页面被访问到,需要先使用artisan命令创建软连接到public目录,另外在使用store方法时传递第二个参数使用public,默认使用local,使用public用于用户能够通过web页面访问
//用户更新操作
    public function update(Request $request)
    {
        if($request->hasFile('profile')) {
            $path = $request->file('profile')->store('/image/'.date('Ymd'),'public');
            $user->profile = asset('storage/'.$path);
        }
       if($user->save()){
           return redirect('/user/index')->with('info','更新成功');
       }else{
           return back()->with('info','更新失败');
       }
    }
配置信任代理
App\Http\Middleware\TrustProxies中间件允许自定义需要被应用信任的代理或负载均衡器。位于$proxies属性列表
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;
class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     * 在这里配置
     * 
     * @var array
     */
    protected $proxies = [
        '127.0.0.1',
        '192.168.1.1',
    ];
    /*配置信任所有代理*/
    protected $proxies = '*';
    /**
     * The headers that should be used to detect proxies.
     * 配置代理发送的带有请求来源信息的消息头
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}