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 ...
随机推荐
- Python3:numpy模块中的argsort()函数
Python3:numpy模块中的argsort()函数 argsort函数是Numpy模块中的函数: >>> import numpy >>> help(nu ...
- [nodejs]在mac环境下如何将node更新至最新?
在mac下安装angular-cli时,报出较多错误.初步怀疑是因为node环境版本过低导致. 在mac下,需要执行如下几步将node更新至最新版本,也可以更新到指定版本 1. sudo npm ca ...
- [luogu4054 JSOI2009] 计数问题(2D BIT)
传送门 Solution 2D BIT模板 Code //By Menteur_Hxy #include <cmath> #include <cstdio> #include ...
- Silverlight之我见——DataGrid数据验证
<UserControl x:Class="DataValidationSample.MainPage" xmlns="http://schemas.microso ...
- https://github.com/MediaTek-Labs/linkit-smart-7688-feed编译失败
mkdir -p /home/fly/workdir/LinkltSmart7688Duo-20170626/openwrt/dl/home/fly/workdir/LinkltSmart7688Du ...
- 【codeforces 768D】Jon and Orbs
[题目链接]:http://codeforces.com/contest/768/problem/D [题意] 你有一个水晶; 它每天都会产生一个球??(球有k种) 然后每种球产生的可能性是相同的-& ...
- ES6的let和var声明变量的区别
关于let的描述 let允许你声明一个作用域被限制在块级中的变量.语句或者表达式.与var关键字不同的是,它声明的变量只能是全局或者整个函数块的. 作用域规则 let声明的变量只在其声明的块或子块中可 ...
- log4net的相关使用笔记
1, XmlConfigurator 创建添加一个Tracer project,引用nuget上最新的log4net 在Tracer里新增一个AppLog类: public static class ...
- JS 仿淘宝幻灯片 非完整版 小案例
仿淘宝幻灯片,基础版,后期效果是要做到每次点击小圆点,切换都无缝 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" &quo ...
- [RxJS 6] The Retry RxJs Error Handling Strategy
When we want to handle error observable in RxJS v6+, we can use 'retryWhen' and 'delayWhen': const c ...