<article><h1>Laravel 的 HTTP 请求 Request</h1><ul><li><a href="#accessing-the-request">获取请求</a><ul><li><a href="#request-path-and-method">请求路径 & 方法</a></li><li><a href="#psr7-requests">PSR-7 请求</a></li></ul></li><li><a href="#input-trimming-and-normaliation">输入数据的预处理和规范化</a></li><li><a href="#retrieving-input">获取输入数据</a><ul><li><a href="#old-input">旧输入数据</a></li><li><a href="#cookies">Cookies</a></li></ul></li><li><a href="#files">文件资源</a><ul><li><a href="#retrieving-uploaded-files">获取上传文件</a></li><li><a href="#storing-uploaded-files">储存上传文件</a></li></ul></li></ul><p><a name="accessing-the-request"></a></p><h2><a href="#accessing-the-request">获取请求</a></h2><p>要通过依赖注入的方式来获取当前 HTTP 请求的实例,你应该在控制器方法中使用 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 类型提示。当前的请求实例将通过 <a href="/docs/5.4/container">服务容器</a> 自动注入:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 储存一个新用户。
*
* @param Request $request
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">store<span class="token punctuation">(</span></span>Request <span class="token variable">$request</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">input<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><h4>依赖注入 & 路由参数</h4><p>如果控制器方法也有输入数据是从路由参数中传入的,只需将路由参数置于其他依赖之后。 例如,你的路由是这样定义的:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'user/{id}'</span><span class="token punctuation">,</span> <span class="token string">'UserController@update'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>只要像下方一样定义控制器方法,你就可以使用 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 类型提示了,同时获取到路由参数 <code class=" language-php">id</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 更新指定的用户。
*
* @param Request $request
* @param string $id
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">update<span class="token punctuation">(</span></span>Request <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token variable">$id</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><h4>通过路由闭包获取请求</h4><p>你也可以在一个路由闭包中使用 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 类型提示。当它执行时,服务容器会自动注入当前请求到闭包中:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span>
<span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>Request <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="request-path-and-method"></a></p><h3>请求路径 & 方法</h3><p><code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 的实例提供了多种方法去检查应用程序的 HTTP 请求,Laravel 的 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 继承了 <code class=" language-php">Symfony\<span class="token package">Component<span class="token punctuation">\</span>HttpFoundation<span class="token punctuation">\</span>Request</span></code> 类。下面是该类几个有用的方法:</p><h4>获取请求路径</h4><p><code class=" language-php">path</code> 方法返回请求路径信息。所以,如果收到的请求目标地址是 <code class=" language-php">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>domain<span class="token punctuation">.</span>com<span class="token operator">/</span>foo<span class="token operator">/</span>bar</code>,那么 <code class=" language-php">path</code> 将会返回 <code class=" language-php">foo<span class="token operator">/</span>bar</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$uri</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php">is</code> 方法可以验证收到的请求路径和指定规则是否匹配。使用这个方法的时候你也可以传递一个 <code class=" language-php"><span class="token operator">*</span></code> 字符作为通配符:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'admin/*'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><h4>获取请求的 URL</h4><p>你可以使用 <code class=" language-php">url</code> 或 <code class=" language-php">fullUrl</code> 方法去获取传入请求的完整 URL。<code class=" language-php">url</code> 方法返回不带有查询字符串的 URL,而 <code class=" language-php">fullUrl</code> 方法的返回值包含查询字符串:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">// Without Query String...
</span><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">url<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// With Query String...
</span><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">fullUrl<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>获取请求方法</h4><p>对于传入的请求 <code class=" language-php">method</code> 方法将返回 HTTP 的请求方式。你也可以使用 <code class=" language-php">isMethod</code> 方法去验证 HTTP 的请求方式与指定规则是否相配:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$method</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">method<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">isMethod<span class="token punctuation">(</span></span><span class="token string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><p><a name="psr7-requests"></a></p><h3>PSR-7 请求</h3><p><a href="http://www.php-fig.org/psr/psr-7/">PSR-7 标准</a>制定的 HTTP 消息接口包含了请求和响应。如果你想使用一个 PSR-7 请求来代替一个 Laravel 请求,那么你首先要安装几个函数库。Laravel 使用了 Symfony 的 HTTP 消息桥接组件,将原来的 Laravel 请求和响应转换到 PSR-7 所兼容的实现:</p><pre class=" language-php"><code class=" language-php">composer <span class="token keyword">require</span> symfony<span class="token operator">/</span>psr<span class="token operator">-</span>http<span class="token operator">-</span>message<span class="token operator">-</span>bridge
composer <span class="token keyword">require</span> zendframework<span class="token operator">/</span>zend<span class="token operator">-</span>diactoros</code></pre><p>安装完这些库后, 就可以在路由闭包或控制器中,简单的对请求类型使用类型提示来获取 PSR-7 的请求:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Psr<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Message<span class="token punctuation">\</span>ServerRequestInterface</span><span class="token punctuation">;</span>
<span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>ServerRequestInterface <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 如果你从路由或者控制器返回了一个 PSR-7 响应实例,那么这个实例将被自动转换回一个 Laravel 响应实例,同时由框架显示。</p></blockquote><p><a name="input-trimming-and-normaliation"></a></p><h2><a href="#input-trimming-and-normaliation">输入数据的预处理和规范化</a></h2><p>在 Laravel 的全局中间件中默认包含了 <code class=" language-php">TrimStrings</code> 和 <code class=" language-php">ConvertEmptyStringsToNull</code> 两个中间件。这些中间件被列在 <code class=" language-php">App\<span class="token package">Http<span class="token punctuation">\</span>Kernel</span></code> 类中。它们会自动处理所有请求中传入的字符串字段,比如将空的字符串字段转变成 <code class=" language-php"><span class="token keyword">null</span></code> 值。你再也不用担心路由和控制器中数据规范化的问题。</p><p>如果你想停用这些功能,你可以在 <code class=" language-php">App\<span class="token package">Http<span class="token punctuation">\</span>Kernel</span></code> 类的 <code class=" language-php"><span class="token variable">$middleware</span></code> 属性中移除这些中间件。</p><p><a name="retrieving-input"></a></p><h2><a href="#retrieving-input">获取输入数据</a></h2><h4>获取所有输入数据</h4><p>你可以使用 <code class=" language-php">all</code> 方法以 <code class=" language-php">数组</code> 形式获取到所有输入数据:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>获取指定输入值</h4><p>你可以通过 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 的实例,借助几个简单的方法,就可以获取到用户输入的所有数据。而不需要担心发起请求时使用了哪一种请求方式,<code class=" language-php">input</code> 方法通常被用来获取用户输入数据:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">input<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你可以给 <code class=" language-php">input</code> 方法的第二个参数传入一个默认值。当请求的输入数据不存在于此请求时,返回该默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">input<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'Sally'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果传输表单数据中包含「数组」形式的数据,那么可以使用「点」语法来获取数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">input<span class="token punctuation">(</span></span><span class="token string">'products.0.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$names</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">input<span class="token punctuation">(</span></span><span class="token string">'products.*.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>通过动态属性获取输入数据</h4><p>你也可以通过 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 实例的动态属性来获取用户输入的数据。例如,如果你应用程序表单中包含 <code class=" language-php">name</code> 字段,那么可以像这样访问提交的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">name</span><span class="token punctuation">;</span></code></pre><p>Laravel 在处理动态属性的优先级是,先从请求的数据中查找,没有的话再到路由参数中找。</p><h4>获取 JSON 输入信息</h4><p>当你发送 JSON 请求到应用时,只要请求表头中设置了 <code class=" language-php">Content<span class="token operator">-</span>Type</code> 为 <code class=" language-php">application<span class="token operator">/</span>json</code>,你就可以直接从 <code class=" language-php">Input</code> 方法中获取 JSON 数据。你也可以通过 「点」语法来读取 JSON 数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">input<span class="token punctuation">(</span></span><span class="token string">'user.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>获取部分输入数据</h4><p>如果你需要获取输入数据的子集,则可以用 <code class=" language-php">only</code> 和 <code class=" language-php">except</code> 方法。这两个方法都接收单个 <code class=" language-php">数组</code> 或动态列表作为参数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">only<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'username'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">only<span class="token punctuation">(</span></span><span class="token string">'username'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">except<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'credit_card'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">except<span class="token punctuation">(</span></span><span class="token string">'credit_card'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php">only</code> 方法会返回所有你指定的键值对,即使这个键在输入数据中并不存在。如果一个键在输入数据中并不存在时,它对应的值是 <code class=" language-php"><span class="token keyword">null</span></code> 。当你想要获取请求中实际存在的输入数据时,你可以使用 <code class=" language-php">intersect</code> 方法。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">intersect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'username'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>确定是否有输入值</h4><p>要判断请求是否存在该数据,可以使用 <code class=" language-php">has</code> 方法。当数据存在 <strong>并且</strong> 字符串不为空时,<code class=" language-php">has</code> 方法就会返回 <code class=" language-php"><span class="token boolean">true</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">has<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><p><a name="old-input"></a></p><h3>旧输入数据</h3><p>Laravel 允许你将本次的输入数据保留到下一次请求发送前。这个特性在表单验证错误后重新填写表单相当有用。但是,如果你使用了 Laravel 的 <a href="/docs/5.4/validation">验证特性</a>,你就不需要在手动实现这些方法,因为 Laravel 内置的验证工具会自动调用他们。</p><h4>将输入数据闪存至 Session</h4><p><code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 的 <code class=" language-php">flash</code> 方法会将当前输入的数据存进 <a href="/docs/5.4/session">session</a> 中,因此下次用户发送请求到应用程序时就可以使用它们:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flash<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以使用 <code class=" language-php">flashOnly</code> 和 <code class=" language-php">flashExcept</code> 方法将当前请求数据的子集保存到 session。这些方法对敏感信息的保护非常有用:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flashOnly<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'username'</span><span class="token punctuation">,</span> <span class="token string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flashExcept<span class="token punctuation">(</span></span><span class="token string">'password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>闪存输入数据到 Session 后重定向</h4><p>你可能需要把输入数据闪存到 session 并重定向到前一个页面,这时只需要在重定向方法后加上 <code class=" language-php">withInput</code> 即可:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'form'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withInput<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'form'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">withInput<span class="token punctuation">(</span></span>
<span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">except<span class="token punctuation">(</span></span><span class="token string">'password'</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>获取旧输入数据</h4><p>若要获取上一次请求后所闪存的输入数据,则可以使用 <code class=" language-php">Request</code> 实例中的 <code class=" language-php">old</code> 方法。<code class=" language-php">old</code> 方法提供一个简便的方式从 <a href="/docs/5.4/session">Session</a> 取出被闪存的输入数据:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$username</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">old<span class="token punctuation">(</span></span><span class="token string">'username'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>Laravel 也提供了全局辅助函数 <code class=" language-php">old</code>。如果你要在 <a href="/docs/5.4/blade">Blade 模板</a> 中显示旧输入数据,可以使用更加方便的 <code class=" language-php">old</code> 辅助函数。如果旧数据不存在,则返回 <code class=" language-php"><span class="token keyword">null</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>text<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>username<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>{{ old('username') }}<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span></code></pre><p><a name="cookies"></a></p><h3>Cookies</h3><h4>从请求中获取 Cookie 值</h4><p>Laravel 框架创建的每个 cookie 都会被加密并且加上认证标识,这代表着用户擅自更改的 cookie 都会失效。若要从此次请求获取 cookie 值,你可以使用 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 实例中的 <code class=" language-php">cookie</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>将 Cookies 附加到响应</h4><p>你可以使用 <code class=" language-php">cookie</code> 方法附加一个 cookie 到 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Response</span></code> 实例。有效 cookie 应该传递字段名称,字段值和过期时间给这个方法:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">response<span class="token punctuation">(</span></span><span class="token string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">cookie<span class="token punctuation">(</span></span>
<span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php">cookie</code> 方法还可以接受更多参数,只是使用频率较低。通常作用是传递参数给 PHP 原生 <a href="http://php.net/manual/en/function.setcookie.php">设置 cookie</a> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">response<span class="token punctuation">(</span></span><span class="token string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">cookie<span class="token punctuation">(</span></span>
<span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">,</span> <span class="token variable">$path</span><span class="token punctuation">,</span> <span class="token variable">$domain</span><span class="token punctuation">,</span> <span class="token variable">$secure</span><span class="token punctuation">,</span> <span class="token variable">$httpOnly</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>生成 Cookie 实例</h4><p>如果你想要在一段时间以后生成一个可以给定 <code class=" language-php">Symfony\<span class="token package">Component<span class="token punctuation">\</span>HttpFoundation<span class="token punctuation">\</span>Cookie</span></code> 的响应实例,你可以先生成 $cookie 实例,然后再指定给 response 实例,否则这个 cookie 不会被发送回客户端:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$cookie</span> <span class="token operator">=</span> <span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token function">response<span class="token punctuation">(</span></span><span class="token string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token variable">$cookie</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote><p>译者注: 关于 Cookie,需要注意一点,默认 Laravel 创建的所有 Cookie 都是加密过的,创建未加密的 Cookie 的方法请见 <a href="https://laravel-china.org/topics/1758">【小技巧分享】在 Laravel 中设置没有加密的 cookie</a></p></blockquote><p><a name="files"></a></p><h2><a href="#files">文件资源</a></h2><p><a name="retrieving-uploaded-files"></a></p><h3>获取上传文件</h3><p>你可以使用 <code class=" language-php">Illuminate\<span class="token package">Http<span class="token punctuation">\</span>Request</span></code> 实例中的 <code class=" language-php">file</code> 方法获取上传的文件。file 方法返回的对象是 <code class=" language-php">Symfony\<span class="token package">Component<span class="token punctuation">\</span>HttpFoundation<span class="token punctuation">\</span>File<span class="token punctuation">\</span>UploadedFile</span></code> 类的实例,该类继承了 PHP 的 <code class=" language-php">SplFileInfo</code> 类,并提供了许多和文件交互的方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$file</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">file<span class="token punctuation">(</span></span><span class="token string">'photo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$file</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">photo</span><span class="token punctuation">;</span></code></pre><p>你可以使用请求的 <code class=" language-php">hasFile</code> 方法确认上传的文件是否存在:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">hasFile<span class="token punctuation">(</span></span><span class="token string">'photo'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><h4>确认上传的文件是否有效</h4><p>除了检查上传的文件是否存在外,你也可以通过 <code class=" language-php">isValid</code> 方法验证上传的文件是否有效:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">file<span class="token punctuation">(</span></span><span class="token string">'photo'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">isValid<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><h4>文件路径 & 扩展</h4><p><code class=" language-php">UploadedFile</code> 这个类也包含了访问文件完整路径和扩展的方法。<code class=" language-php">extension</code> 方法会尝试根据文件内容猜测文件的扩展名。猜测结果可能不同于客户端原始的扩展名:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">photo</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$extension</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">photo</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">extension<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>其它上传文件的方法</h4><p><code class=" language-php">UploadedFile</code> 的实例还有许多可用的方法,可以到该对象的 <a href="http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html">API 文档</a> 了解这些方法的详细信息。</p><p><a name="storing-uploaded-files"></a></p><h3>储存上传文件</h3><p>在设置好 <a href="/docs/5.4/filesystem">文件系统</a> 的配置信息后,你可以使用 <code class=" language-php">UploadedFile</code> 的 <code class=" language-php">store</code> 方法把上传文件储存到本地磁盘,或者是亚马逊 S3 云存储上。</p><p><code class=" language-php">store</code> 方法允许存储文件到相对于文件系统根目录配置的路径。这个路径不能包含文件名,名称将使用 MD5 散列文件内容自动生成。</p><p><code class=" language-php">store</code> 方法还接受一个可选的第二个参数,用于文件存储到磁盘的名称。这个方法会返回文件相对于磁盘根目录的路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">photo</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">store<span class="token punctuation">(</span></span><span class="token string">'images'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">photo</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">store<span class="token punctuation">(</span></span><span class="token string">'images'</span><span class="token punctuation">,</span> <span class="token string">'s3'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果你不想自动生成文件名,那么可以使用 <code class=" language-php">storeAs</code> 方法去设置路径,文件名和磁盘名作为方法参数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">photo</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">storeAs<span class="token punctuation">(</span></span><span class="token string">'images'</span><span class="token punctuation">,</span> <span class="token string">'filename.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">photo</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">storeAs<span class="token punctuation">(</span></span><span class="token string">'images'</span><span class="token punctuation">,</span> <span class="token string">'filename.jpg'</span><span class="token punctuation">,</span> <span class="token string">'s3'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度