博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Laravel请求/Cookies/文件上传
阅读量:6901 次
发布时间:2019-06-27

本文共 3095 字,大约阅读时间需要 10 分钟。

一.HTTP请求

1.基本示例:

通过依赖注入获取当前 HTTP 请求实例,应该在控制器的构造函数或方法中对Illuminate\Http\Request 类进行
类型提示,当前请求实例会被服务容器自动注入

控制器中:

public function store(Request $request){	$name=$request->input('name');	//}

  

如果还需要获取路由参数输入,只需要将路由参数置于其他依赖之后,例如你的路由定义如下

Route::put('user/{id}','UserController@update');

  

在控制器中:

public function update(Request $request,$id){	//}

  

2.基本请求信息

a.$uri=$request->path(); //返回请求的URI,,如果进入的请求路径是http://domain.com/foo/bar ,则path 方法将
会返回foo/bar
b.$request->is('admin/*'); //is 方法允许你验证进入的请求是否与给定模式匹配。使用该方法时可以使用* 通配符
c.$url=$request->url(); //获取完整的url
d.获取请求方法及判断:

$method=$request->method();if($request->isMethod('post')){	//}

二.获取输入

1.输入参数

a.$name = $request->input('name','default'); //获取用户输入的name,如果没有值,用default作为默认值
b.$input = $request->input('products.0.name'); //处理表单数组输入时,可以使用”.”来访问数组
c.$request->has('name'); //判断输入值是否出现
d.$input = $request->all(); //获取所有数据
e.获取部分数据
$input = $request->only('username', 'password');
$input = $request->except('credit_card');

2.上一次请求输入:

a.将输入存储到一次性 Session:
$request->flash(); //全部存入
$request->flashOnly('username', 'email'); //仅存入username,email
$request->flashExcept('password'); //除了password,全部存入

b.将输入存储到一次性 Session 然后重定向

return redirect('form')->withInput();
return redirect('form')->withInput($request->except('password'));

3.取出上次数据
$all = $request->old();
$username = $request->old('username');

模板中使用全局函数old

{
{ old('username') }}

三.Cookies

1.请求cookie
$value = $request->cookie('name');
也可以使用辅助方法
$value = Request::cookie('name');
$value = Cookie::get('name');

2.新增cookie

Cookie::queue('test', 'Hello, Laravel', 10, '/'); // 添加一个名为test的cookie,值为'Hello, Laravel',有效期10分钟示例:public function index(Request $request){	Cookie::queue('test2', 'Hello, Laravel', 10);        return 'ok';}

  

response示例

$response = new \Illuminate\Http\Response();$response->withCookie(cookie('name', 'valuefff', 10));return $response;

  

3.设置永久cookie(5年有效期)

示例

$response = new \Illuminate\Http\Response();$response->withCookie(Cookie::forever('test10', 'Hello, Laravel'));return $response;

  

如果需要response,还要相应模板,可以使用,Response::view

$cookie = Cookie::forever('test29', 'Hello, Laravel');return \Response::view('test.hi')->withCookie($cookie);

  

4.清除cookie

$cookie = Cookie::forget('test25');return \Response::view('test.hi')->withCookie($cookie);

  

四.文件上传

1.创建
$file = $request->file('photo');

2.$request->hasFile('photo'),判断文件在请求中是否存在

3.验证文件是否在上传中出错

$request->file('photo')->isValid()

4.保存

$request->file('photo')->move($destinationPath);
$request->file('photo')->move($destinationPath, $fileName);

完整示例:

//文件上传处理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('文件上传成功!');}

转载于:https://www.cnblogs.com/itfenqing/p/6919809.html

你可能感兴趣的文章
iscroll手册
查看>>
5.Struts2框架中的ServletAPI如何获取
查看>>
java学习路线
查看>>
layui-学习01-基本api
查看>>
Angular 表单验证 基础篇
查看>>
大话设计模式第十三章---建造者模式
查看>>
Google Chrome浏览器的使用方法
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource
查看>>
『左偏树 Leftist Tree』
查看>>
打印菱形
查看>>
因数分解
查看>>
HDU 6092 Rikka with Subset 【dp多重背包】【好题】
查看>>
.NET学习笔记 01
查看>>
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.3 Displaying Classes in a Layer...
查看>>
Eclipse下egit插件的使用
查看>>
[NOI2017]整数
查看>>
LINUX信息安全系统设计基础第一周学习总结
查看>>
xdebug对php程序性能分析
查看>>
surfaceview 透明
查看>>
osi七层模型
查看>>