laravel陌生知識點快速學習一

來源:互聯網
上載者:User
laravel陌生知識點

php參數的預設值

functionmakecoffee($type = "cappuccino"){return"Making a cup of $type.\n";}echo makecoffee();echo makecoffee(null);echo makecoffee("espresso");?>

輸出

Making a cup of cappuccino.Making a cup of .Making a cup of espresso.

模型繫結 (Model Binding)

在RouteServiceProvider中,在boot方法裡實現模型繫結

public function boot(Router $router)    {        parent::boot($router);        $router->model('users', 'App\User');        $router->model('goods', 'App\Good');        $router->model('categories', 'App\Category');        $router->model('tryClothes', 'App\TryRecord');        $router->model('carts', 'App\Cart');        $router->model('orders', 'App\Order');        $router->model('orderItems', 'App\OrderItem');        //    }

表單申請 (Form Request)

  • 使用以下指令產生自訂Request
phpartisanmake:requestCreateArticleRequest
  • 自訂Request中的方法:authorize() 和 rules();authorize判斷是否有許可權,rules進行資料驗證
publicfunctionauthorize()    {returntrue;    }
publicfunctionrules(){return [        'title' => 'required|min:3',        'body' => 'required',        'published_at' => 'required|date',        // 也可以使用數組//'published_at' => ['required', 'date'],    ];}
  • 使用Request的方法中通常是傳入了POST資料,之所以定義自訂Request類是為了複用代碼與解除耦合性,完全可以使用Validate類進行自訂Request中rules方法的處理
publicfunctionstore(Request $request){$this->validate($request, ['title' => 'required|min:3', 'body' =>'required', 'published_at' => 'required|date']);        Article::create($request->all());        return redirect('articles');    }
  • 如果通過驗證,可以使用$request->all()直接將資料給相關的類
Article::create($request->all());

php storm laravel 代碼提示

  • https://gist.githubusercontent.com/barryvdh/5227822/raw/811f21a14875887635bb3733aef32da51fa0501e/_ide_helper.php
  • 記得在.gitignore檔案中添加這個檔案

在特定檔案夾中建立控制器

php artisan make:controller Console/ConsoleController
  • 注意在routes.php事先寫的代碼沒有問題,否則出現了以下錯誤
[ReflectionException]  Class App\Http\Controllers\console does not exist

參考資料

  • Laravel 5.0 - Form Requests
  • http://9iphp.com/web/laravel/laravel-5-form-request-controller-validation.html
  • laracast

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

以上就介紹了laravel陌生知識點快速學習一,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.