說明:本文主要講述Laravel容器事件,並更根據容器事件作一個簡單demo供加深理解容器事件。同時,做者會將開發過程當中的一些截圖和代碼黏上去,提升閱讀效率。php
Container是Laravel框架的核心,Container中儲存着各類各樣的Service,而且每個Service經過Service Provider註冊在Container裏,經過Facade模式幫咱們從容器裏去解析須要的Service對象。而這個過程當中,容器每一次從容器中解析對象時是會觸發一個事件的,能夠經過resolving
方法監聽到。實際上在Laravel框架中表單請求驗證就用到這個好工具,經過一個表單請求類來實現表單內容驗證,以避免把邏輯放在控制器里弄亂控制器,具體能夠看中文文檔:表單請求驗證。關於Container Event能夠看文檔:容器事件。css
先寫路由:html
Route::post('containerevent', 'ContainerEventController@containerEvent'); Route::post('formrequest', 'ContainerEventController@formRequest'); Route::get('container', 'ContainerEventController@profile');
再建個控制器:jquery
php artisan make:controller ContainerEventController
寫上方法:laravel
public function containerEvent() { } public function formRequest() { } public function profile() { return view('profile'); }
寫上view:bootstrap
<html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! --> <title>Bootstrap Template</title> <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"> <style> html,body{ width: 100%; height: 100%; } *{ margin: 0; border: 0; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-xs-12 col-md-12"> <form action="/formrequest" method="post" accept-charset="UTF-8" enctype="multipart/form-data"> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" id="name" name="name" placeholder="Name"> </div> <div class="form-group"> <label for="age">Age</label> <input type="text" class="form-control" id="age" name="age" placeholder="Age"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> </div> <!-- jQuery文件。務必在bootstrap.min.js 以前引入 --> <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <script> </script> </body> </html>
寫個表單請求類:先輸入命令生成表單請求類app
php artisan make:request ContainerFormRequest
再給出驗證規則框架
class ContainerFormRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true;//改成true,這裏通常用做用戶驗證,這裏所有經過驗證 } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required', 'age' => 'required|numeric|min:18', ]; } }
修改ContainerEventController:ide
public function formRequest(ContainerFormRequest $containerFormRequest) { dd('This is a From Request Container Event. It is working!!!'); }
同時把app/Http/Kernel.php文件中\App\Http\Middleware\VerifyCsrfToken::class註銷掉,不然提交表單TokenMismatchException錯誤。
工具
好,輸入路由(修改成你的路由):http://laravelcontainerevent.app:8888/container,則輸入錯誤表單會返回到當前表單頁面,正確提交輸入表單後會打印:
說明fromRequest已經工做了,ContainerFormRequest這個對象從容器中解析的時候,會先工做authorize
和rules
方法。而控制器中只須要注入ContainerFormRequest這個對象就好了。
實現一個自定義的類,實現表單提交相同的功能。在app/Contracts文件夾中新建EventBeforeResolving.php文件:
namespace App\Contracts; interface EventBeforeResolving { public function isAdult(); }
並在一個service provider註冊下:
//AppServiceProvider.php /** * Register any application services. * * @return void */ public function register() { //當從容器中解析注入到控制器中前,會先調用實現EventBeforeResolving接口對象的isAdult()方法 $this->app->resolving(EventBeforeResolving::class,function($obj, $app){ $obj->isAdult(); }); }
寫一個service實現這個接口:
//app/Services/Authorize.php namespace App\Services; use App\Contracts\EventBeforeResolving; use Illuminate\Http\Request; class Authorize implements EventBeforeResolving { private $request; public function __construct(Request $request) { $this->request = $request; } public function isAdult() { $name = $this->request->input('name'); $age = $this->request->input('age'); if(empty($name) || empty($age) || ($age < 18)){ dd('Name and Age must be required. And Age must be above 18'); } } }
修改下ContainerEventController:
public function containerEvent(Authorize $authorize) { dd('This is a Demo Container Event. It is working!!!'); }
同時別忘了修改下profile.blade.php文件中表單提交action='/containerevent'。
當輸入錯誤時會提示錯誤信息:
Container Event就是在Service對象從容器中解析注入前觸發事件,能夠利用這個功能作一些有趣又好用的好東西呢,好比Laravel框架的表單請求驗證就是這麼作的,這樣不會把驗證邏輯代碼放在控制器中,以避免弄亂控制器。
總結:本節主要講述Laravel的容器事件,並以Form Requet爲例說明它的用途,同時以一個小demo講述怎麼一步步創建並監聽容器事件。嘛,過兩天還想結合Laravel的Task Scheduler任務調度新開篇章,到時見。