Laravel5.1学习笔记5 请求
HTTP 请求
#取得请求实例
#基本的请求信息
#PSR-7 请求
#取出输入数据
#旧的输入
#Cookies
#文件
#取得请求实例(此部分文档5.1完全重写,注意)
要通过依赖注入获取当前HTTP Request的实例, 你应该在控制器构造器,或方法中 type-hint (类型约束)Illuminate\Http\Request 类, 当前request 实例会被服务容器自动注入:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
如果你的控制器方法也期待着路由参数的输入,只需在你的其他依赖注入后面列出你的路由参数,比如你的路由这样定义:
Route::put('user/{id}', 'UserController@update');
Illuminate\Http\Request 和通过定义控制器方法取得你的路由参数Id
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Update the specified user.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
}
#基本的请求信息
Illuminate\Http\Request 实例提供了不同的方法去验证HTTP请求, Laravel 的 Illuminate\Http\Request 继承了Symfony\Component\HttpFoundation\Request 类, 这里有几个更有用的类方法:
取得 请求Request的 URI
Path方法返回请求的URI, 因此,如果请求的目标是 http://domain.com/foo/bar, Path方法会返回 foo/bar:
$uri = $request->path();
if ($request->is('admin/*')) {
//
}
$url = $request->url();
获得Request对象的方法
$method = $request->method();
if ($request->isMethod('post')) {
//
}
PSR-7 Requests
PSR-7标准制定了HTTP信息的接口, 包括Requests 和 Responses, 如果你想要获得一个PSR-7请求的实例,你首先需要安装一些库, Laravel使用Symfony HTTP Message Bridge 组件去把Laravel Requests和 Response转为PSR-7兼容的实现。
composer require symfony/psr-http-message-bridge
composer require zendframework/zend-diactoros
use Psr\Http\Message\ServerRequestInterface;
Route::get('/', function (ServerRequestInterface $request) {
//
});
#取得输入数据
取得特定输入数据
你可以通过 Illuminate\Http\Request 的实例,经由几个简洁的方法取得所有的用户输入数据。不需要担心发出请求时使用的 HTTP 请求,取得输入数据的方式都是相同的。
$name = Request::input('name');
取得特定输入数据,若没有则取得默认值
$name = Request::input('name', 'Sally');
$input = $request->input('products.0.name');
确认是否有输入数据,用has方法,返回true如果有值且不为空。
if (Request::has('name'))
{
//
}
取得所有发出请求时传入的输入数据
$input = Request::all();
取得部分发出请求时传入的输入数据
$input = Request::only('username', 'password');
$input = Request::except('credit_card');
#旧输入数据
Laravel 可以让你保留这次的输入数据,直到下一次请求发送前。例如,你可能需要在表单验证失败后重新填入表单值。
将输入数据存成一次性 Session
Illuminate\Http\Request实例中的flash 方法会将当前的输入数据存进 session中,所以下次用户发出请求时可以使用保存的数据:
$request->flash();
将部分输入数据存成一次性 Session
Request::flashOnly('username', 'email');
Request::flashExcept('password');
快闪到Session然后重定向
你很可能常常需要在重定向至前一页,并将输入数据存成一次性 Session。只要在重定向方法后的链式调用方法中传入输入数据,就能简单地完成。
return redirect('form')->withInput();
return redirect('form')->withInput(Request::except('password'));
取得旧输入数据
若想要取得前一次请求所保存的一次性 Session,你可以使用 Request 实例中的 old 方法。
$username = $request->old('username');
old :{{ old('username') }}
#Cookies
Laravel 所建立的 cookie 会加密并且加上认证记号,这代表着被用户擅自更改的 cookie 会失效。从请求中取得Cookie值,你使用cookie方法
$value = $request->cookie('name');
还可以使用辅助方法
$value = Request::cookie('name');
加上新的 Cookie 到响应
辅助方法 cookie 提供一个简易的工厂方法来产生新的 Symfony\Component\HttpFoundation\Cookie 实例。可以在 Response 实例之后连接 withCookie 方法带入 cookie 至响应:
$response = new Illuminate\Http\Response('Hello World');
$response->withCookie(cookie('name', 'value', $minutes));
return $response;
建立永久有效的 Cookie*
虽然说是「永远」,但真正的意思是五年。
$response->withCookie(cookie()->forever('name', 'value'));
Queueing Cookies
You may also "queue" a cookie to be added to the outgoing response, even before that response has been created:
<?php namespace App\Http\Controllers;
use Cookie;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Update a resource
*
* @return Response
*/
public function update()
{
Cookie::queue('name', 'value');
return response('Hello World');
}
}
上传文件
取得上传文件
$file = $request->file('photo');
确认文件是否有上传
if (Request::hasFile('photo'))
{
//
}
file 方法返回的对象是 Symfony\Component\HttpFoundation\File\UploadedFile 的实例,UploadedFile 继承了 PHP 的 SplFileInfo 类并且提供了很多和文件交互的方法。
确认上传的文件是否有效
if (Request::file('photo')->isValid())
{
//
}
移动上传的文件
这个move方法从暂时目录移动文件到一个你指定的永久目录, (PHP配置决定暂时目录)
Request::file('photo')->move($destinationPath);
Request::file('photo')->move($destinationPath, $fileName);
其他上传文件的方法
UploadedFile 的实例还有许多可用的方法,可以至 API文档 了解有关这些方法的详细信息。
(以下内容5.1文档被删,只存在5.0文档中)
#其他的请求信息
Request 类提供很多方法检查 HTTP 请求,它继承了 Symfony\Component\HttpFoundation\Request类,下面是一些使用方式。
取得请求 URI
$uri = Request::path();
判断一个请求是否使用了 AJAX
if (Request::ajax())
{
//
}
取得请求方法
$method = Request::method();
if (Request::isMethod('post'))
{
//
}
确认请求路径是否符合特定格式
if (Request::is('admin/*'))
{
//
}
取得请求 URL
$url = Request::url();
Laravel5.1学习笔记5 请求的更多相关文章
- Django:学习笔记(4)——请求与响应
Django:学习笔记(4)——请求与响应 0.URL路由基础 Web应用中,用户通过不同URL链接访问我们提供的服务,其中首先经过的是一个URL调度器,它类似于SpringBoot中的前端控制器. ...
- Laravel5.1学习笔记9 系统架构1 请求生命周期 (待修)
Request Lifecycle Introduction Lifecycle Overview Focus On Service Providers Introduction When using ...
- Tornado学习笔记(三) 请求方式/状态码
本章我们来学习 Tornado 支持的请求方式 请求方式 Tornado支持任何合法的HTTP请求(GET.POST.PUT.DELETE.HEAD.OPTIONS).你可以非常容易地定义上述任一种方 ...
- iOS学习笔记---网络请求
一.HTTP协议的概念 HTTP协议:Hyper Text Transfer Protocol(超文本传输协议)是用于从万维网服务器传送超文本到本地浏览器的传输协议.HTTP是一个应用层协议,由请求和 ...
- angular2 学习笔记 ( Http 请求)
refer : https://angular.cn/docs/ts/latest/guide/server-communication.html https://xgrommx.github.io/ ...
- java web Servlet学习笔记-2 请求重定向和请求转发的区别
请求转发与请求重定向的区别 请求重定向和转发 1.请求重定向:浏览器的行为(通过响应对象HttpServletResponse来执行) 特点:可以重新定向访问其他Web应用下的资源 浏览器发出了2次请 ...
- Laravel5.1学习笔记19 EloquentORM 入门
Eloquent:入门 简介 定义模型(model) Eloquent 模型规范 取出多个模型 取出单个模型 / 集合 取出集合 插入更新模型 基本插入 基本更新 大批量赋值 删除模型 软删除 查询 ...
- Laravel5.1学习笔记18 数据库4 数据填充
简介 编写数据填充类 使用模型工厂类 调用额外填充类 执行填充 #简介 Laravel includes a simple method of seeding your database with t ...
- Laravel5.1学习笔记i14 系统架构6 Facade
Facades 介绍 使用 Facades Facade 类参考 #介绍 Facades provide a "static" interface to classes th ...
随机推荐
- Miller Rabbin素数测试
步骤 ①先写快速幂取模函数 ②MR算法开始 (1)传入两个参数一个是底数一个是n也就是幂数,如果n是一个合数那么可以判定,这个数一定不是素数 (2)然后开始寻找一个奇数的n去计算,如果最后满足a^d% ...
- CentOS 7.2安装配置Vsftp服务器
一.配置防火墙,开启FTP服务器需要的端口 CentOS 7.2默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...
- 《零压力学Python》 之 第一章知识点归纳
第一章(初识Python)知识点归纳 Python是从ABC语言衍生而来的 ABC语言是Guido参与设计的一种教学语言,为非专业编程人员所开发的. Python是荷兰程序员 Guido Van Ro ...
- PAT 1108 Finding Average
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 8.2.3 操作MySQL数据库
Python访问MySQL数据库可以使用MySQLDb模块,该模块主要方法如下: (1)commit():提交事务. (2)rollback():回滚事务. (3)callproc(self,proc ...
- 解析特殊格式的xml到map
由于项目特殊,需要解析的xml文档样式特别,所以自己写了一个解析特殊xml的方法 先提供xml样式 <?xml version="1.0" encoding="UT ...
- 2017沈阳网络赛hdu6199 gems gems gems
gems gems gems Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) P ...
- libcloud代码研究(三)——bugs
Bug 1:对不可迭代类进行迭代(libcloud.storage.driver.cloudfile line. 141-142) 使用libcloud连接自搭建swift服务,自己在服务器 ...
- Oracle-统计数据库表数据总数量
create or replace procedure prc_table_count(p_flag out varchar2) AS TCOUNT number; SCOUNT number; CO ...
- HDU 5239 上海大都会 D题(线段树+数论)
打表,发现规律是存在一定次数(较小)后,会出现a=(a*a)%p.可以明显地发现本题与线段树有关.设置标记flag,记录本段内的数是否均已a=a*a%p.若是,则不需更新,否则更新有叶子结点,再pus ...