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 ...
随机推荐
- 联想小新Air 15 安装黑苹果macOS High Sierra 10.13.6过程
联想小新Air 15 安装黑苹果全过程 本文参考:https://blog.csdn.net/qq_28735663/article/details/80634300 本人是联想小新AIr 15 , ...
- (ccf)201709-4 通信网络
#include<iostream> #include<memory.h> #include<stack> #include<string> #incl ...
- Python基础-奇偶判断调用函数
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数 1/1+1/3+...+1/n. 首先写一个n为偶数的函数: def peven(n): s = 0 ...
- 手机浏览PC版页面出现背景图片显示不全的问题解决方案
手机浏览PC版页面出现背景图片显示不全 给定宽高的值
- Selenium的安装和简单实用——PhantomJS安装
简介 Selenium是一个用于Web应用程序测试的工具. Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Firefox,S ...
- 利用Calendar类测试电脑运行速度
今天学习了很多新知识! 这里使用了Calender类来获取系统时间,并计算循环1w次的时间,判断电脑处理时间. import java.util.Calendar; public class Arra ...
- 洛谷 P2195 HXY造公园
P2195 HXY造公园 题目描述 现在有一个现成的公园,有n个休息点和m条双向边连接两个休息点.众所周知,HXY是一个SXBK的强迫症患者,所以她打算施展魔法来改造公园并即时了解改造情况.她可以进行 ...
- Uva 1331 - Minimax Triangulation(最优三角剖分 区间DP)
题目大意:依照顺时针或者逆时针的顺序给出多边的点,要将这个多边形分解成n-2个三角形,要求使得这些三角行中面积最大的三角形面积尽量小,求最小值. 思路:用区间DP能够非常方便解决,多边形可能是凹边形, ...
- [Cypress] Test XHR Failure Conditions with Cypress
Testing your application’s behavior when an XHR call results in an error can be difficult. The use o ...
- ios 导航栏(自己定义和使用系统方式)
系统方式: //1.设置导航栏背景图片 [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] ini ...