Laravel框架学习(Request请求数据、Cookie、文件上传)
来源:互联网 发布:dos攻击域名 编辑:程序博客网 时间:2024/05/28 11:29
1、获取Request请求数据
Laravel中一般通过控制器方法依赖注入来获取当前请求的Request实例。
我们通过定义一个隐式控制器来进行本章节的测试。首先我们在routes.php定义路由如下:
Route::controller('request','RequestController');
然后我们在app/Http/Controllers下创建一个控制器RequestController.php:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Http\Response;use App\Http\Requests;use App\Http\Controllers\Controller;class RequestController extends Controller{ public function getBasetest(Request $request) { $input = $request->input('test'); echo $input; }}
要访问getBasetest方法,我们只需在浏览器中访问http://selfstudy.com/request/basetest?test=yyk,这样页面会输出:
yyk, 注意url里的basetest方法对应的是控制器中的getBasetest方法
2、获取请求URL及请求方法
2.1 获取请求URL路径
要获取当前请求的URL,我们可以通过Request实例上的path方法,需要注意的是该方法返回相对请求路径,如果要获取绝对请求路径,可以通过Request实例上的另一个方法——url:
public function getUrl(Request $request){ //匹配request/*的URL才能继续访问 if(!$request->is('request/*')){ abort(404); } $uri = $request->path(); $url = $request->url(); echo $uri.'--'.$url;}
我们在浏览器中访问http://selfstudy.com/request/url,页面输入如下内容:
request/url–http://selfstudy.com/request/url
2.2 获取请求方法
我们还可以通过调用Request实例上的getMethod方法获取当前请求的方法:
public function getMethod(Request $request){ //非get请求不能访问 if(!$request->isMethod('get')){ abort(404); } $method = $request->method(); echo $method;}
3、获取请求数据
3.1 当前请求输入
使用Request实例上的input方法即可获取请求输入数据。该方法可以接收两个参数,第一个参数是传递参数名称,第二个参数是如果参数名为空返回的默认值,此外该方法还支持获取数组参数对应值,我们定义测试方法如下:
public function getInputData(Request $request){ //获取GET方式传递的name参数,默认为LaravelAcademy $name = $request->input('name','LaravelAcademy'); echo $name; echo '<br>'; echo $request->input('test.0.name');}
注意这里方法参数的写法 input-data对应的控制器中的方法为getInputData
在浏览器中输入http://selfstudy.com/request/input-data?name=abc&test[][name]=abcd,则页面输出:
abc
abcd
如果我们想要在获取输入值之前判断输入参数名是否存在,可以使用has方法,比如我们想要判断输入参数是否包含hello,可使用如下方法:
if($request->has('hello')) echo $request->input('hello');
想要获取所有输入参数值,可以使用Request实例上的all方法;想要获取部分输入值,可使用only方法;想要排除部分输入参数值,可使用except方法:
http://selfstudy.com/request/input-data?name=abc&test[][name]=abcd&hello=world
public function getInputData(Request $request){ $allData = $request->all(); $onlyData = $request->only('name','hello'); $exceptData = $request->except('hello'); echo '<pre>'; print_r($allData); print_r($onlyData); print_r($exceptData);}
3.2 上一次请求输入
上面的方法都是用于获取当前请求的输入,如果想要获取上一次请求的输入,需要在处理上一次请求时使用Request实例上的flash方法将请求数据暂时保存到session中,然后在当前请求中使用Request实例上的old方法获取session中保存的数据,获取到数据后就会将session中保存的请求数据销毁:
public function getLastRequest(Request $request){ $request->flash();}public function getCurrentRequest(Request $request){ $lastRequestData = $request->old(); echo '<pre>'; print_r($lastRequestData);}
如果我们想要在上次请求保存数据后重定向到当前请求URL,则可以使用如下方式定义getLastRequest方法:
public function getLastRequest(Request $request){ //$request->flash(); return redirect('/request/current-request')->withInput();}
在浏览器中输入http://selfstudy.com/request/last-request?name=abc&test[][name]=abcd&hello=world页面会重定向到http://selfstudy.com/request/current-request并输入出
Array ( [name] => abc [test] => Array ( [0] => Array ( [name] => abcd
) ) [hello] => world )
再次刷新页面,输出为空:
Array ( )
4、获取Cookie
我们可以使用Request实例上的cookie方法获取cookie数据,该方法可以接收一个参数名返回对应的cookie值,如果不传入参数,默认返回所有cookie值:
public function getCookie(Request $request){ $cookies = $request->cookie(); dd($cookies);}
我们在浏览器中访问http://selfstudy.com/request/cookie,页面输出:
array:2 [ "XSRF-TOKEN" => "eXdbBOXXkNwiJMlvmCG29lTMpKl92xnTtORHtgNf" "laravel_session" => "618cd5ab8c0560b14cda49bc19a8d6d63dc26cc3"]
我们可以调用Response实例上的withCookie方法新增cookie:
public function getAddCookie(){ $response = new Response(); //第一个参数是cookie名,第二个参数是cookie值,第三个参数是有效期(分钟) $response->withCookie(cookie('test','yykk',1)); //如果想要cookie长期有效使用如下方法 //$response->withCookie(cookie()->forever('name', 'value')); return $response;}
我们重新定义getCookie方法如下:
public function getCookie(Request $request){ $cookie = $request->cookie('test'); echo $cookie;}
接下来我们在浏览器中访问http://selfstudy.com/request/add-cookie,再访问http://selfstudy.com/request/cookie,页面输出如下:
yykk
5、文件上传
我们定义文件上传页面及上传处理如下:
//文件上传表单public function getFileupload(){ $postUrl = '/request/fileupload'; $csrf_field = csrf_field(); $html = <<<CREATE<form action="$postUrl" method="POST" enctype="multipart/form-data">$csrf_field<input type="file" name="file"><br/><br/><input type="submit" value="提交"/></form>CREATE; return $html;}//文件上传处理public function postFileupload(Request $request){ //判断请求中是否包含name=file的上传文件 if(!$request->hasFile('file')){ exit('上传文件为空!'); } $file = $request->file('file'); //判断文件上传过程中是否出错 if(!$file->isValid()){ exit('文件上传出错!'); } $destPath = realpath(public_path('images')); if(!file_exists($destPath)) mkdir($destPath,0755,true); $filename = $file->getClientOriginalName(); if(!$file->move($destPath,$filename)){ exit('保存文件失败!'); } exit('文件上传成功!');}
通过以上代码可以看到我们可以使用Request实例上的file方法获取上传文件实例,该方法接收的参数是上传文件input标签的name属性,该文件上传实例是 Symfony\Component\HttpFoundation\File\UploadedFile类的实例,更多有关该实例的可用方法,可参考UploadedFile的API文档。
- Laravel框架学习(Request请求数据、Cookie、文件上传)
- Laravel框架学习(Request请求数据、Cookie、文件上传)
- laravel 框架的文件上传
- laravel框架简单文件上传
- request学习---文件上传
- php laravel 框架 之文件上传
- laravel框架-实现简单的文件上传
- laravel(二)laravel文件上传
- laravel上传文件报错:413 Request Entity Too Large
- laravel_基础_Request请求/Cookie/文件上传
- laravel框架数据迁移、填充(简单示例)--学习笔记
- laravel框架学习(路由)
- Laravel框架学习(CSRF)
- Laravel框架学习(中间件)
- Laravel框架学习(Response)
- laravel框架学习(一)
- laravel 框架学习(二)
- 快捷使用Retrofit网络请求框架(get请求,post请求,上传文件)
- 美术常见问题汇总
- 《汇编语言》(王爽)检测点2.1
- file_get_contents 超时 参数 stream_context_create()
- 敌兵布阵(线段树)
- 1033. To Fill or Not to Fill (25)
- Laravel框架学习(Request请求数据、Cookie、文件上传)
- 网络是怎样连接的学习笔记(六)
- Tensorflow入门2(模型训练,模型评估)
- USACO 2017 February Contest Silver
- YOLO视频检测
- (PTA)7-1 整数分解为若干项之和
- js操作DOM的各种方式
- 量化交易----入门认识
- java中Array Set List互转问题