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

Laravel请求/Cookies/文件上传的更多相关文章

  1. Django 10 GET和POST(HttpRequest对象,GET和POST请求,文件上传,HttpResponse对象的cookie)

    Django 10 GET和POST(HttpRequest对象,GET和POST请求,文件上传,HttpResponse对象的cookie) 一.HttpRequest对象 #HttpRequest ...

  2. Android okHttp网络请求之文件上传下载

    前言: 前面介绍了基于okHttp的get.post基本使用(http://www.cnblogs.com/whoislcj/p/5526431.html),今天来实现一下基于okHttp的文件上传. ...

  3. php laravel 帧 该文件上传

    好,我承认我的忠告. 今天laravel框架编写一个文件上传部分.总能找到不正确的路径.但是,终于攻克. 以下我分享一下自己的学习体会吧. client <form method="P ...

  4. 使用HttpClient 发送 GET、POST、PUT、Delete请求及文件上传

    package org.caeit.cloud.dev.util; import java.io.File; import java.io.IOException; import java.io.Un ...

  5. Laravel 5 教程 - 文件上传

    一.简介 Laravel 有很棒的文件系统抽象层,是基于 Frank de Jonge 的 Flysystem 扩展包. Laravel 集成的 Flysystem 提供了简单的接口,可以操作本地端空 ...

  6. Android 普通okhttp、okhttp utils执行 post get请求,文件上传下载、请求图片

    public class OKHttpActivity extends Activity implements View.OnClickListener { public static final M ...

  7. SpringCloud 之 Fegin —— 发送GET、POST请求以及文件上传

    由于项目需要调用其他微服务的数据,首先想到的就是写一个http网络请求的工具类,但是想到在之前看springCloud的时候里面有这个Fegin可以实现,就顺便实践一下,虽然过程有点坎坷,好在都顺利解 ...

  8. 前端笔记之微信小程序(三)GET请求案例&文件上传和相册API&配置https

    一.信息流小程序-GET请求案例 1.1服务端接口开发 一定要养成接口的意识,前端单打独斗出不来任何效果,必须有接口配合,写一个带有分页.关键词查询的接口: 分页接口:http://127.0.0.1 ...

  9. 使用HttpClient 发送 GET、POST(FormData、Raw)、PUT、Delete请求及文件上传

    httpclient4.3.6 package org.caeit.cloud.dev.util; import java.io.File; import java.io.IOException; i ...

随机推荐

  1. 汕头市队赛 SRM10 T1模拟只会猜题意

    模拟只会猜题意 SRM 10 描述 有一本n个单词的词典,求按下列方法能造出的不相同的词汇数目.  1.词典中的单词是一个词. 2.能分为两部分的,其中前一部分是一个词典词或者其非空前缀,后一部分是一 ...

  2. laravel 学习笔记 —— 神奇的服务容器

    转载自:https://www.insp.top/learn-laravel-container 容器,字面上理解就是装东西的东西.常见的变量.对象属性等都可以算是容器.一个容器能够装什么,全部取决于 ...

  3. [ CodeVS冲杯之路 ] P1501

     不充钱,你怎么AC? 题目:http://codevs.cn/problem/1501/ 水题一道 直接dfs,记录上当前深度,到了叶子节点就更新答案,并且每个节点将当前深度的计数+1,答案即为ma ...

  4. [bzoj1001][BJOI2006]狼抓兔子——最大流转最短路,平面图

    题目描述: 给定一个平面图,求最小割. 题解: 本题是一道经典题. 周冬Orz的论文是很好的研究资料. 这道题点太多,所以直接跑dinic无疑会超时. 我们观察原图,发现原图是一个平面图. 什么是平面 ...

  5. MFC 将 '当前工作路径' 改为 'exe所在路径'(转)

    原文转自 https://blog.csdn.net/morewindows/article/details/8683519 调整进程当前目录为程序可执行文件所在目录是个非常实用的方法.为了更加的让代 ...

  6. 第1章 Windows程序内部运行机制

    参考: https://blog.csdn.net/u014162133/article/details/46573873 1.Windows API与Win32 SDK 操作系统提供了各种方便开发W ...

  7. 嵌入式Linux支持LCD console【转】

    转自:http://blog.sina.com.cn/s/blog_664c545f0100v9zl.html 转载:http://www.mculee.cn/post/48.html [1]LCD ...

  8. 华为上机测试题(及格分数线-java)

    PS:自己写的,自测试OK,供大家参考. /* 描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:(1) 及格线是10的倍数:(2) 保证至少有60%的学生及格:(3) 如果所有 ...

  9. 使用百度地图JavaScript实现驾车/公交/步行导航功能

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  10. Ubuntu安装java环境

    Ubuntu安装java环境 1.添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装oracle-j ...