首頁 > 後端開發 > php教程 > laravel陌生知識點快速學習一

laravel陌生知識點快速學習一

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2016-08-08 09:19:57
原創
832 人瀏覽過

laravel陌生知識點

php參數的預設值

<code><span><span><?php</span><span><span>function</span><span>makecoffee</span><span>(<span>$type</span> = <span>"cappuccino"</span>)</span>
{</span><span>return</span><span>"Making a cup of $type.\n"</span>;
}
<span>echo</span> makecoffee();
<span>echo</span> makecoffee(<span>null</span>);
<span>echo</span> makecoffee(<span>"espresso"</span>);
<span>?></span></span></code>
登入後複製

輸出

<code>Making <span>a</span> cup <span>of</span> cappuccino.
Making <span>a</span> cup <span>of</span> .
Making <span>a</span> cup <span>of</span> espresso.</code>
登入後複製

模型綁定(Model Binding)

在RouteServiceProvider中,在boot方法申請中實作模型

使用以下指令產生自訂Request
  • <code><span>public</span> function boot(Router <span>$router</span>)
        {
            <span>parent</span><span>::boot</span>(<span>$router</span>);
            <span>$router</span><span>-></span>model(<span>'users'</span>, <span>'App\User'</span>);
            <span>$router</span><span>-></span>model(<span>'goods'</span>, <span>'App\Good'</span>);
            <span>$router</span><span>-></span>model(<span>'categories'</span>, <span>'App\Category'</span>);
            <span>$router</span><span>-></span>model(<span>'tryClothes'</span>, <span>'App\TryRecord'</span>);
            <span>$router</span><span>-></span>model(<span>'carts'</span>, <span>'App\Cart'</span>);
            <span>$router</span><span>-></span>model(<span>'orders'</span>, <span>'App\Order'</span>);
            <span>$router</span><span>-></span>model(<span>'orderItems'</span>, <span>'App\OrderItem'</span>);
            <span>//</span>
        }</code>
    登入後複製
自訂Request中的方法:authorize() 和rules();authorize判斷是否有權限,rules進行資料驗證
  • <code><span>php</span><span>artisan</span><span>make</span><span>:request</span><span>CreateArticleRequest</span></code>
    登入後複製
    <code><span>public</span><span><span>function</span><span>authorize</span><span>()</span>
        {</span><span>return</span><span>true</span>;
        }</code>
    登入後複製
    通常是傳入了POST數據,之所以定義自訂Request類別是為了重複使用程式碼與解除耦合性,完全可以使用Validate類別進行自訂Request中rules方法的處理
<code><span>public</span><span><span>function</span><span>rules</span><span>()</span>
{</span><span>return</span> [
        <span>'title'</span> => <span>'required|min:3'</span>,
        <span>'body'</span> => <span>'required'</span>,
        <span>'published_at'</span> => <span>'required|date'</span>,
        <span>// 也可以使用数组</span><span>//'published_at' => ['required', 'date'],</span>
    ];
}</code>
登入後複製
  • 如果通過驗證,可以使用$request->all()直接將資料給相關的類別
<code><span>public</span><span><span>function</span><span>store</span><span>(Request <span>$request</span>)</span>{</span><span>$this</span>->validate(<span>$request</span>, [<span>'title'</span> => <span>'required|min:3'</span>, <span>'body'</span> =><span>'required'</span>, <span>'published_at'</span> => <span>'required|date'</span>]);
        Article::create(<span>$request</span>->all());
        <span>return</span> redirect(<span>'articles'</span>);
    }</code>
登入後複製
    php storm laravel 程式碼提示
https://gist.githubusercontent.com/barryvdh/5227822/raw/811f21a148758305227822/raw/811f21a1487583038758300000000 php

記得在.gitignore檔案中加入這個檔案
  • 在特定資料夾中建立控制器
  • <code>Article<span>::create</span>(<span>$request</span><span>-></span><span>all</span>());</code>
    登入後複製
注意在routes.php事先寫的程式碼沒有問題,否則出現了以下錯誤

<code>php artisan <span>make</span>:controller Console/ConsoleController</code>
登入後複製
    參考資料
  • 5.0 - Form Requests
http://9iphp.com/web/laravel/laravel-5-form-request-controller-validation.html

laracast
  • laracast

laracast

🎜laracast🎜經博主允許不得轉載。 🎜🎜 🎜 以上就介紹了laravel陌生知識點快速學習一,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。 🎜 🎜 🎜
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板