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)
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教程有興趣的朋友有所協助。