<article><h1>Laravel 的辅助函数列表</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#available-methods">可用方法</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Laravel 包含有各种各样的 PHP 辅助函数,许多都是在 Laravel 自身框架中使用到。如果你觉得实用,也可以在你自己的应用中使用它们。</p><p><a name="available-methods"></a></p><h2><a href="#available-methods">可用方法</a></h2><style>.collection-method-list > p {
column-count: 3; -moz-column-count: 3; -webkit-column-count: 3;
column-gap: 2em; -moz-column-gap: 2em; -webkit-column-gap: 2em;
}
.collection-method-list a {
display: block;
}</style><h3>数组</h3><div class="collection-method-list"><p><a href="#method-array-add">array_add</a> <a href="#method-array-collapse">array_collapse</a> <a href="#method-array-divide">array_divide</a> <a href="#method-array-dot">array_dot</a> <a href="#method-array-except">array_except</a> <a href="#method-array-first">array_first</a> <a href="#method-array-flatten">array_flatten</a> <a href="#method-array-forget">array_forget</a> <a href="#method-array-get">array_get</a> <a href="#method-array-has">array_has</a> <a href="#method-array-last">array_last</a> <a href="#method-array-only">array_only</a> <a href="#method-array-pluck">array_pluck</a> <a href="#method-array-prepend">array_prepend</a> <a href="#method-array-pull">array_pull</a> <a href="#method-array-set">array_set</a> <a href="#method-array-sort">array_sort</a> <a href="#method-array-sort-recursive">array_sort_recursive</a> <a href="#method-array-where">array_where</a> <a href="#method-head">head</a> <a href="#method-last">last</a></p></div><h3>路径</h3><div class="collection-method-list"><p><a href="#method-app-path">app_path</a> <a href="#method-base-path">base_path</a> <a href="#method-config-path">config_path</a> <a href="#method-database-path">database_path</a> <a href="#method-mix">mix</a> <a href="#method-public-path">public_path</a> <a href="#method-resource-path">resource_path</a> <a href="#method-storage-path">storage_path</a></p></div><h3>字符串</h3><div class="collection-method-list"><p><a href="#method-camel-case">camel_case</a> <a href="#method-class-basename">class_basename</a> <a href="#method-e">e</a> <a href="#method-ends-with">ends_with</a> <a href="#method-snake-case">snake_case</a> <a href="#method-str-limit">str_limit</a> <a href="#method-starts-with">starts_with</a> <a href="#method-str-contains">str_contains</a> <a href="#method-str-finish">str_finish</a> <a href="#method-str-is">str_is</a> <a href="#method-str-plural">str_plural</a> <a href="#method-str-random">str_random</a> <a href="#method-str-singular">str_singular</a> <a href="#method-str-slug">str_slug</a> <a href="#method-studly-case">studly_case</a> <a href="#method-title-case">title_case</a> <a href="#method-trans">trans</a> <a href="#method-trans-choice">trans_choice</a></p></div><h3>URLs</h3><div class="collection-method-list"><p><a href="#method-action">action</a> <a href="#method-asset">asset</a> <a href="#method-secure-asset">secure_asset</a> <a href="#method-secure-url">secure_url</a> <a href="#method-route">route</a> <a href="#method-url">url</a></p></div><h3>其他</h3><div class="collection-method-list"><p><a href="#method-abort">abort</a> <a href="#method-abort-if">abort_if</a> <a href="#method-abort-unless">abort_unless</a> <a href="#method-auth">auth</a> <a href="#method-back">back</a> <a href="#method-bcrypt">bcrypt</a> <a href="#method-cache">cache</a> <a href="#method-collect">collect</a> <a href="#method-config">config</a> <a href="#method-csrf-field">csrf_field</a> <a href="#method-csrf-token">csrf_token</a> <a href="#method-dd">dd</a> <a href="#method-dispatch">dispatch</a> <a href="#method-env">env</a> <a href="#method-event">event</a> <a href="#method-factory">factory</a> <a href="#method-info">info</a> <a href="#method-logger">logger</a> <a href="#method-method-field">method_field</a> <a href="#method-old">old</a> <a href="#method-redirect">redirect</a> <a href="#method-request">request</a> <a href="#method-response">response</a> <a href="#method-retry">retry</a> <a href="#method-session">session</a> <a href="#method-value">value</a> <a href="#method-view">view</a></p></div><p><a name="method-listing"></a></p><h2><a href="#method-listing">方法列表</a></h2><style>#collection-method code {
font-size: 14px;
}
#collection-method:not(.first-collection-method) {
margin-top: 50px;
}</style><p><a name="arrays"></a></p><h2><a href="#arrays">数组</a></h2><p><a name="method-array-add"></a></p><h4 id="collection-method" class="first-collection-method"><code class=" language-php"><span class="token function">array_add<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>如果给定的键不存在与数组中,<code class=" language-php">array_add</code> 就会把给定的键值对添加到数组中:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_add<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['name' => 'Desk', 'price' => 100]</span></code></pre><p><a name="method-array-collapse"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_collapse<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_collapse</code> 函数把数组里的每一个数组合并成单个数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_collapse<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></code></pre><p><a name="method-array-divide"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_divide<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_divide</code> 函数返回两个数组,一个包含原本数组的键,另一个包含原本数组的值:</p><pre class=" language-php"><code class=" language-php"><span class="token function">list<span class="token punctuation">(</span></span><span class="token variable">$keys</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">array_divide<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// $keys: ['name']
</span><span class="token comment" spellcheck="true">
// $values: ['Desk']</span></code></pre><p><a name="method-array-dot"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_dot<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_dot</code> 函数把多维数组压制成一维数组,并用「点」式语法表示深度:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_dot<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'bar'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'baz'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['foo.bar' => 'baz'];</span></code></pre><p><a name="method-array-except"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_except<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_except</code> 函数从数组移除指定的键值对:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_except<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['name' => 'Desk']</span></code></pre><p><a name="method-array-first"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_first<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_first</code> 函数返回数组中第一个通过指定测试的元素:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_first<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 200</span></code></pre><p>可传递第三个参数作为默认值。当没有元素通过测试时,将会返回该默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_first<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-array-flatten"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_flatten<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_flatten</code> 函数将多维数组压制成一维数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Joe'</span><span class="token punctuation">,</span> <span class="token string">'languages'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'PHP'</span><span class="token punctuation">,</span> <span class="token string">'Ruby'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_flatten<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['Joe', 'PHP', 'Ruby'];</span></code></pre><p><a name="method-array-forget"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_forget<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_forget</code> 函数以「点」式语法从深度嵌套的数组中移除指定的键值对:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'products'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'desk'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token function">array_forget<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'products.desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['products' => []]</span></code></pre><p><a name="method-array-get"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_get<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_get</code> 函数使用「点」式语法从深度嵌套的数组中获取指定的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'products'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'desk'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_get<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'products.desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['price' => 100]</span></code></pre><p><code class=" language-php">array_get</code> 函数同样也接受默认值,如果指定的键找不到时,则返回该默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_get<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'names.john'</span><span class="token punctuation">,</span> <span class="token string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-array-has"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_has<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_has</code> 函数使用「点」式语法检查指定的项目是否存在于数组中:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$hasItem</span> <span class="token operator">=</span> <span class="token function">array_has<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'product.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true
</span>
<span class="token variable">$hasItems</span> <span class="token operator">=</span> <span class="token function">array_has<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'product.price'</span><span class="token punctuation">,</span> <span class="token string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// false</span></code></pre><p><a name="method-array-last"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_last<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_last</code> 函数返回数组中最后一个通过指定测试的元素:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">110</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_last<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 300</span></code></pre><p><a name="method-array-only"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_only<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_only</code> 函数从数组返回指定的键值对:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'orders'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_only<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['name' => 'Desk', 'price' => 100]</span></code></pre><p><a name="method-array-pluck"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_pluck<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_pluck</code> 函数从数组拉出一列指定的键值对:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'developer'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Taylor'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'developer'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Abigail'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_pluck<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'developer.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['Taylor', 'Abigail'];</span></code></pre><p>你也可以指定要以什么作为结果列的键名:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_pluck<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'developer.name'</span><span class="token punctuation">,</span> <span class="token string">'developer.id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1 => 'Taylor', 2 => 'Abigail'];</span></code></pre><p><a name="method-array-prepend"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_prepend<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_prepend</code> 函数将元素加到数组的头部:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'one'</span><span class="token punctuation">,</span> <span class="token string">'two'</span><span class="token punctuation">,</span> <span class="token string">'three'</span><span class="token punctuation">,</span> <span class="token string">'four'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_prepend<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'zero'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// $array: ['zero', 'one', 'two', 'three', 'four']</span></code></pre><p><a name="method-array-pull"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_pull<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_pull</code> 函数从数组移除指定键值对并返回该键值对:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</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 function">array_pull<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// $name: Desk
</span><span class="token comment" spellcheck="true">
// $array: ['price' => 100]</span></code></pre><p><a name="method-array-set"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_set<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_set</code> 函数使用「点」式语法在深度嵌套的数组中写入值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'products'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'desk'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token function">array_set<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['products' => ['desk' => ['price' => 200]]]</span></code></pre><p><a name="method-array-sort"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_sort<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_sort</code> 函数根据指定闭包的结果排序数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_values<span class="token punctuation">(</span></span><span class="token function">array_sort<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span><span class="token punctuation">[</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 punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['name' => 'Chair'],
['name' => 'Desk'],
]
*/</span></code></pre><p><a name="method-array-sort-recursive"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_sort_recursive<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_sort_recursive</code> 函数使用 sort 函数递归排序数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token punctuation">[</span>
<span class="token string">'Roman'</span><span class="token punctuation">,</span>
<span class="token string">'Taylor'</span><span class="token punctuation">,</span>
<span class="token string">'Li'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span>
<span class="token string">'PHP'</span><span class="token punctuation">,</span>
<span class="token string">'Ruby'</span><span class="token punctuation">,</span>
<span class="token string">'JavaScript'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_sort_recursive<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
[
'Li',
'Roman',
'Taylor',
],
[
'JavaScript',
'PHP',
'Ruby',
]
];
*/</span></code></pre><p><a name="method-array-where"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">array_where<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">array_where</code> 函数使用指定的闭包过滤数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'200'</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token string">'400'</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_where<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">is_string<span class="token punctuation">(</span></span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1 => 200, 3 => 400]</span></code></pre><p><a name="method-head"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">head<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">head</code> 函数返回指定数组的第一个元素:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$first</span> <span class="token operator">=</span> <span class="token function">head<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 100</span></code></pre><p><a name="method-last"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">last<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">last</code> 函数返回指定数组的最后一个元素:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$last</span> <span class="token operator">=</span> <span class="token function">last<span class="token punctuation">(</span></span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 300</span></code></pre><p><a name="paths"></a></p><h2><a href="#paths">路径</a></h2><p><a name="method-app-path"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">app_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">app_path</code> 函数返回 <code class=" language-php">app</code> 文件夹的完整路径。你也可以使用 <code class=" language-php">app_path</code> 函数生成针对指定文件相对于 app 目录的完整路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">app_path<span class="token punctuation">(</span></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 function">app_path<span class="token punctuation">(</span></span><span class="token string">'Http/Controllers/Controller.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-base-path"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">base_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">base_path</code> 函数返回项目根目录的完整路径。你也可以使用 <code class=" language-php">base_path</code> 函数生成针对指定文件相对于项目根目录的完整路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">base_path<span class="token punctuation">(</span></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 function">base_path<span class="token punctuation">(</span></span><span class="token string">'vendor/bin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-config-path"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">config_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">config_path</code> 函数返回 <code class=" language-php">config</code> 目录的完整路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">config_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-database-path"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">database_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">database_path</code> 函数返回 <code class=" language-php">database</code> 目录的完整路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">database_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-mix"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">mix<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">mix</code> 函数获取带有版本号的 <a href="/docs/5.4/mix">mix</a> 文件:</p><pre class=" language-php"><code class=" language-php"><span class="token function">mix<span class="token punctuation">(</span></span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-public-path"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">public_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">public_path</code> 函数返回 <code class=" language-php"><span class="token keyword">public</span></code> 目录的完整路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">public_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-resource-path"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">resource_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">resource_path</code> 函数返回 <code class=" language-php">resources</code> 目录的完整路径。你也可以使用 <code class=" language-php">resource_path</code> 函数生成针对指定文件相对于 <code class=" language-php">resources</code> 目录的完整路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">resource_path<span class="token punctuation">(</span></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 function">resource_path<span class="token punctuation">(</span></span><span class="token string">'assets/sass/app.scss'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-storage-path"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">storage_path<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">storage_path</code> 函数返回 <code class=" language-php">storage</code> 目录的完整路径。你也可以使用 <code class=" language-php">storage_path</code> 函数生成针对指定文件相对于 <code class=" language-php">storage</code> 目录的完整路径:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">storage_path<span class="token punctuation">(</span></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 function">storage_path<span class="token punctuation">(</span></span><span class="token string">'app/file.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="strings"></a></p><h2><a href="#strings">字符串</a></h2><p><a name="method-camel-case"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">camel_case<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">camel_case</code> 函数将指定的字符串转换成 <code class=" language-php">驼峰式命名</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$camel</span> <span class="token operator">=</span> <span class="token function">camel_case<span class="token punctuation">(</span></span><span class="token string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// fooBar</span></code></pre><p><a name="method-class-basename"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">class_basename<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">class_basename</code> 函数返回不包含命名空间的类名称:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$class</span> <span class="token operator">=</span> <span class="token function">class_basename<span class="token punctuation">(</span></span><span class="token string">'Foo\Bar\Baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// Baz</span></code></pre><p><a name="method-e"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">e<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">e</code> 函数对指定字符串进行 <code class=" language-php">htmlentities</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">e<span class="token punctuation">(</span></span>'<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span></span>foo<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span></span>'<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// &lt;html&gt;foo&lt;/html&gt;</span></code></pre><p><a name="method-ends-with"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">ends_with<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">ends_with</code> 函数判断指定字符串结尾是否为指定内容:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">ends_with<span class="token punctuation">(</span></span><span class="token string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true</span></code></pre><p><a name="method-snake-case"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">snake_case<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">snake_case</code> 函数将指定的字符串转换成 <code class=" language-php">蛇形命名</code> :</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$snake</span> <span class="token operator">=</span> <span class="token function">snake_case<span class="token punctuation">(</span></span><span class="token string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// foo_bar</span></code></pre><p><a name="method-str-limit"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_limit<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_limit</code> 函数限制字符串的字符个数,该函数接受一个字符串作为第一个参数,第二个参数为允许的最大字符个数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_limit<span class="token punctuation">(</span></span><span class="token string">'The PHP framework for web artisans.'</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// The PHP...</span></code></pre><p><a name="method-starts-with"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">starts_with<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">starts_with</code> 函数判断字符串开头是否为指定内容:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">starts_with<span class="token punctuation">(</span></span><span class="token string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string">'This'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true</span></code></pre><p><a name="method-str-contains"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_contains<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_contains</code> 函数判断字符串是否包含有指定内容:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_contains<span class="token punctuation">(</span></span><span class="token string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string">'my'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true</span></code></pre><p>你也可以传递数组,来判断字符串是否包任意指定内容:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_contains<span class="token punctuation">(</span></span><span class="token string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'my'</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true</span></code></pre><p><a name="method-str-finish"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_finish<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_finish</code> 函数添加指定内容到字符串末尾:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token function">str_finish<span class="token punctuation">(</span></span><span class="token string">'this/string'</span><span class="token punctuation">,</span> <span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// this/string/</span></code></pre><p><a name="method-str-is"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_is<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_is</code> 函数判断指定的字符串是否匹配指定的格式,星号可作为通配符使用:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_is<span class="token punctuation">(</span></span><span class="token string">'foo*'</span><span class="token punctuation">,</span> <span class="token string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true
</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_is<span class="token punctuation">(</span></span><span class="token string">'baz*'</span><span class="token punctuation">,</span> <span class="token string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// false</span></code></pre><p><a name="method-str-plural"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_plural<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_plural</code> 函数把字符串转换成复数形式。该函数目前只支持英文:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural<span class="token punctuation">(</span></span><span class="token string">'car'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// cars
</span>
<span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural<span class="token punctuation">(</span></span><span class="token string">'child'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// children</span></code></pre><p>你可以传入一个整数作为第二个参数,来获取字符串的单数或复数形式:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural<span class="token punctuation">(</span></span><span class="token string">'child'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// children
</span>
<span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural<span class="token punctuation">(</span></span><span class="token string">'child'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// child</span></code></pre><p><a name="method-str-random"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_random<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_random</code> 函数生成指定长度的随机字符串。该函数使用了 PHP 自带的 <code class=" language-php">random_bytes</code> 函数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token function">str_random<span class="token punctuation">(</span></span><span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-str-singular"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_singular<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_singular</code> 函数把字符串转换成单数形式。该函数目前只支持英文:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token function">str_singular<span class="token punctuation">(</span></span><span class="token string">'cars'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// car</span></code></pre><p><a name="method-str-slug"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">str_slug<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">str_slug</code> 函数根据指定字符串生成 URL 友好的「slug」:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$title</span> <span class="token operator">=</span> <span class="token function">str_slug<span class="token punctuation">(</span></span><span class="token string">'Laravel 5 Framework'</span><span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// laravel-5-framework</span></code></pre><p><a name="method-studly-case"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">studly_case<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">studly_case</code> 函数把指定字符串转换成 <code class=" language-php">首字母大写</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">studly_case<span class="token punctuation">(</span></span><span class="token string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// FooBar</span></code></pre><p><a name="method-title-case"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">title_case<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">title_case</code> 函数把指定字符串转换成 <code class=" language-php">每个单词首字母大写</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$title</span> <span class="token operator">=</span> <span class="token function">title_case<span class="token punctuation">(</span></span><span class="token string">'a nice title uses the correct case'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// A Nice Title Uses The Correct Case</span></code></pre><p><a name="method-trans"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">trans<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">trans</code> 函数根据你的 <a href="/docs/5.4/localization">本地化文件</a> 翻译指定的语句:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">trans<span class="token punctuation">(</span></span><span class="token string">'validation.required'</span><span class="token punctuation">)</span><span class="token punctuation">:</span></code></pre><p><a name="method-trans-choice"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">trans_choice<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">trans_choice</code> 函数根据给定数量来决定翻译指定语句是复数形式还是单数形式:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">trans_choice<span class="token punctuation">(</span></span><span class="token string">'foo.bar'</span><span class="token punctuation">,</span> <span class="token variable">$count</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="urls"></a></p><h2><a href="#urls">URLs</a></h2><p><a name="method-action"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">action<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">action</code> 函数根据指定控制器的方法生成 URL,你不需要传入该控制器的完整命名空间。只需要传入相对于 <code class=" language-php">App\<span class="token package">Http<span class="token punctuation">\</span>Controllers</span></code> 命名空间的控制器类名:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">action<span class="token punctuation">(</span></span><span class="token string">'HomeController@getIndex'</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">$url</span> <span class="token operator">=</span> <span class="token function">action<span class="token punctuation">(</span></span><span class="token string">'UserController@profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-asset"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">asset<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>根据当前请求的协议(HTTP 或 HTTPS)生成资源文件的 URL:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">asset<span class="token punctuation">(</span></span><span class="token string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-secure-asset"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">secure_asset<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>使用 HTTPS 协议生成资源文件的 URL:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">secure_asset<span class="token punctuation">(</span></span><span class="token string">'foo/bar.zip'</span><span class="token punctuation">,</span> <span class="token variable">$title</span><span class="token punctuation">,</span> <span class="token variable">$attributes</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-route"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">route<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">route</code> 函数生成指定路由名称的 URL:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route<span class="token punctuation">(</span></span><span class="token string">'routeName'</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">$url</span> <span class="token operator">=</span> <span class="token function">route<span class="token punctuation">(</span></span><span class="token string">'routeName'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-secure-url"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">secure_url<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">secure_url</code> 数使用 HTTPS 协议生成指定路径的完整 URL:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">secure_url<span class="token punctuation">(</span></span><span class="token string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token function">secure_url<span class="token punctuation">(</span></span><span class="token string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-url"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">url<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">url</code> 函数生成指定路径的完整 URL:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果没有提供路径参数,将会返回一个 <code class=" language-php">Illuminate\<span class="token package">Routing<span class="token punctuation">\</span>UrlGenerator</span></code> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">echo</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">current<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">full<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token function">url<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">previous<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="miscellaneous"></a></p><h2><a href="#miscellaneous">其他</a></h2><p><a name="method-abort"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">abort<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">abort</code> 函数抛出一个将被异常处理句柄渲染的 HTTP 异常:</p><pre class=" language-php"><code class=" language-php"><span class="token function">abort<span class="token punctuation">(</span></span><span class="token number">401</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 function">abort<span class="token punctuation">(</span></span><span class="token number">401</span><span class="token punctuation">,</span> <span class="token string">'Unauthorized.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-abort-if"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">abort_if<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">abort_if</code> 函数如果指定的布尔表达式值为 <code class=" language-php"><span class="token boolean">true</span></code> 则抛出一个 HTTP 异常:</p><pre class=" language-php"><code class=" language-php"><span class="token function">abort_if<span class="token punctuation">(</span></span><span class="token operator">!</span> <span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">isAdmin<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-abort-unless"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">abort_unless<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">abort_unless</code> 函数如果指定的布尔表达式值为 <code class=" language-php"><span class="token boolean">false</span></code> 则抛出一个 HTTP 异常:</p><pre class=" language-php"><code class=" language-php"><span class="token function">abort_unless<span class="token punctuation">(</span></span><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">isAdmin<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-auth"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">auth<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">auth</code> 函数返回一个 authenticator 实例,可以使用它来代替 Auth facade:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">auth<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-back"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">back<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php"><span class="token function">back<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code> 函数生成一个重定向响应让用户返回到之前的位置:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">back<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-bcrypt"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">bcrypt<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">bcrypt</code> 函数使用 Bcrypt 算法哈希指定的数值。你可以使用它代替 <code class=" language-php">Hash</code> facade:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">bcrypt<span class="token punctuation">(</span></span><span class="token string">'my-secret-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-cache"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">cache<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">cache</code> 函数尝试从缓存获取给定 <code class=" language-php">key</code> 的值。如果 <code class=" language-php">key</code> 不存在则返回默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token string">'default'</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 function">cache<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">cache<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'key'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token scope">Carbon<span class="token punctuation">::</span></span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">addSeconds<span class="token punctuation">(</span></span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-collect"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">collect</code> 函数根据指定的数组生成 <a href="/docs/5.4/collections">集合</a> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'taylor'</span><span class="token punctuation">,</span> <span class="token string">'abigail'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-config"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">config<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">config</code> 函数用于获取配置信息的值,配置信息的值可通过「点」式语法访问,其中包含要访问的文件名以及选项名。可传递一个默认值作为第二参数,当配置信息不存在时,则返回该默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config<span class="token punctuation">(</span></span><span class="token string">'app.timezone'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config<span class="token punctuation">(</span></span><span class="token string">'app.timezone'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php">config</code> 辅助函数也可以在运行期间,根据指定的键值对设置指定的配置信息:</p><pre class=" language-php"><code class=" language-php"><span class="token function">config<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'app.debug'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-csrf-field"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">csrf_field<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">csrf_field</code> 函数生成包含 CSRF 令牌内容的 HTML 表单隐藏字段。例如,使用 <a href="/docs/5.4/blade">Blade 语法</a>:</p><pre class=" language-php"><code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">csrf_field<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p><a name="method-csrf-token"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">csrf_token<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">csrf_token</code> 函数获取当前 CSRF 令牌的内容:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token function">csrf_token<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-dd"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">dd<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">dd</code> 函数输出指定变量的值并终止脚本运行:</p><pre class=" language-php"><code class=" language-php"><span class="token function">dd<span class="token punctuation">(</span></span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dd<span class="token punctuation">(</span></span><span class="token variable">$value1</span><span class="token punctuation">,</span> <span class="token variable">$value2</span><span class="token punctuation">,</span> <span class="token variable">$value3</span><span class="token punctuation">,</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果你不想终止脚本运行,使用 <code class=" language-php">dump</code> 函数代替:</p><pre class=" language-php"><code class=" language-php"><span class="token function">dump<span class="token punctuation">(</span></span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-dispatch"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">dispatch<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">dispatch</code> 函数把一个新任务推送到 Laravel 的 <a href="/docs/5.4/queues">任务队列</a>中:</p><pre class=" language-php"><code class=" language-php"><span class="token function">dispatch<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>SendEmails</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-env"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">env<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">env</code> 函数获取环境变量值或返回默认值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'APP_ENV'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// Return a default value if the variable doesn't exist...
</span><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env<span class="token punctuation">(</span></span><span class="token string">'APP_ENV'</span><span class="token punctuation">,</span> <span class="token string">'production'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-event"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">event<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">event</code> 函数派发指定的 <a href="/docs/5.4/events">事件</a> 到所属的侦听器:</p><pre class=" language-php"><code class=" language-php"><span class="token function">event<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-factory"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">factory<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">factory</code> 函数根据指定类、名称以及数量生成模型工厂构造器(model factory builder)。可用于 <a href="/docs/5.4/database-testing#writing-factories">测试</a> 或 <a href="/docs/5.4/seeding#using-model-factories">数据填充</a>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">factory<span class="token punctuation">(</span></span><span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-info"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">info<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">info</code> 函数以 <code class=" language-php">info</code> 级别写入日志:</p><pre class=" language-php"><code class=" language-php"><span class="token function">info<span class="token punctuation">(</span></span><span class="token string">'Some helpful information!'</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 function">info<span class="token punctuation">(</span></span><span class="token string">'User login attempt failed.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-logger"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">logger<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">logger</code> 函数以 <code class=" language-php">debug</code> 级别写入日志:</p><pre class=" language-php"><code class=" language-php"><span class="token function">logger<span class="token punctuation">(</span></span><span class="token string">'Debug message'</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 function">logger<span class="token punctuation">(</span></span><span class="token string">'User has logged in.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果没有传入参数,则会返回一个 <a href="/docs/5.4/errors#logging">日志</a> 的实例:</p><pre class=" language-php"><code class=" language-php"><span class="token function">logger<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">error<span class="token punctuation">(</span></span><span class="token string">'You are not allowed here.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-method-field"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">method_field<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">method_field</code> 函数生成模拟各种 HTTP 动作请求的 HTML 表单隐藏字段。例如,使用 <a href="/docs/5.4/blade">Blade 语法</a>:</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>form</span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>POST<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">method_field<span class="token punctuation">(</span></span><span class="token string">'DELETE'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>form</span><span class="token punctuation">></span></span></span></code></pre><p><a name="method-old"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">old<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">old</code> 函数 <a href="/docs/5.4/requests#retrieving-input">获取</a> session 内一次性的历史输入值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old<span class="token punctuation">(</span></span><span class="token string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old<span class="token punctuation">(</span></span><span class="token string">'value'</span><span class="token punctuation">,</span> <span class="token string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-redirect"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">redirect</code> 函数返回一个 HTTP 重定向响应,如果调用时没有传入参数则返回 redirector 实例:</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">'/home'</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 punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">route<span class="token punctuation">(</span></span><span class="token string">'route.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-request"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">request<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">request</code> 函数返回当前 <a href="/docs/5.4/requests">请求</a> 实例或获取输入的项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token function">request<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">request<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$default</span> <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">)</span></code></pre><p><a name="method-response"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">response<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">response</code> 函数创建一个 <a href="/docs/5.4/responses">响应</a> 实例或获取一个 response 工厂实例:</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 number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</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 punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">json<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-retry"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">retry<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">retry</code> 函数将会重复调用给定的回调函数,最多调用指定的次数。如果回调函数没有抛出异常并且有值返回,则 <code class=" language-php">retry</code> 函数返回该值。如果回调函数抛出异常,<code class=" language-php">retry</code> 函数将拦截异常并自动再次调用回调函数,直到调用给定的次数。如果重试次数超出给定次数,拦截的异常将会抛出:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">retry<span class="token punctuation">(</span></span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // Attempt 5 times while resting 100ms in between attempts...
</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-session"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">session</code> 函数可用于获取或设置单个 session 项:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你可以通过传递键值对数组给该函数设置 session 项:</p><pre class=" language-php"><code class=" language-php"><span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'chairs'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token string">'instruments'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>该函数在没有传递参数时,将返回 session 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">session<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-value"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">value<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">value</code> 函数返回指定数值。而当你传递一个 <code class=" language-php">闭包</code> 给该函数时,该 <code class=" language-php">闭包</code> 将被运行并返回该 <code class=" language-php">闭包</code> 的运行结果:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">value<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">'bar'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-view"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">view<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">view</code> 函数获取 <a href="/docs/5.4/views">视图</a> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'auth.login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://github.com/cmzz">@zyxcba</a></td><td><img class="avatar-66 rm-style" src="https://avatars3.githubusercontent.com/u/6111715?v=3&s=100"></td><td>翻译</td><td><a href="http://pigtk.com">小猪淘客</a> - 免费的淘宝客优惠券CMS</td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度