摘要:本節(jié)將實現(xiàn)文章評論與用戶關(guān)聯(lián)的功能。關(guān)系定義首先修改與表,增加字段增加全部回滾并重新執(zhí)行遷移添加用戶表與文章表評論表的一對多關(guān)系添加文章評論表與用戶表的多對一關(guān)系同時,評論表的字段增加。同時,我們還自定義了返回的錯誤信息。
本節(jié)將實現(xiàn)文章、評論與用戶關(guān)聯(lián)的功能。
關(guān)系定義首先修改 posts 與 comments 表,增加 user_id 字段
/database/migrations/2017_04_12_124622_create_posts_table.php /database/migrations/2017_04_15_062905_create_comments_table.php public function up() { Schema::create("posts", function (Blueprint $table) { // 增加 $table->integer("user_id")->unsigned(); $table->foreign("user_id") ->references("id") ->on("users") ->onDelete("cascade"); }); }
全部回滾并重新執(zhí)行遷移:
$ php artisan migrate:refresh
添加用戶表與文章表、評論表的一對多關(guān)系:
/app/User.php public function posts() { return $this->hasMany(AppPost::class); } public function comments() { return $this->hasMany(AppComment::class); }
添加文章、評論表與用戶表的多對一關(guān)系:
/app/Comment.php /app/Post.php public function user() { return $this->belongsTo(AppUser::class); }
同時,評論表的 $fillable 字段增加 user_id。
注冊首先,定義處理注冊相關(guān)業(yè)務(wù)的控制器:
$ php artisan make:controller RegistrationController
定義路由響應(yīng)注冊請求:
Route::get("/register","RegistrationController@create");
定義方法,返回注冊頁面視圖:
public function create() { return view("registration.create"); }
創(chuàng)建注冊頁面:
/resources/views/registration/create.blade.php @extends("layouts.master") @section("content")@endsection
定義路由響應(yīng)注冊提交:
Route::post("/register","RegistrationController@store");
定義方法處理注冊提交:
/app/Http/Controllers/RegistrationController.php use AppUser; public function store() { $this->validate(request(),[ "name" => "required", "email" => "required|email", "password" => "required|confirmed", ]); $user = User::create(request(["name","password","email"])); auth()->login($user); return redirect()->home(); }
該方法包括了四部分:
驗證字段,其中 password 使用了 confirmed 驗證規(guī)則,會自動去匹配 xxx 與 xxx_confirmation 是否一致,因此之前的視圖要按照規(guī)范命名好。
創(chuàng)建用戶
登錄該用戶
返回名字為「home」的路由
其中,我們需要為路由命名,以匹配第四步:
Route::get("/posts","PostsController@index")->name("home");
雖然完成了注冊功能,但是我們保存密碼使用的明文,我們可以定義一個修改器,讓每次保存密碼時都自動加密:
/app/User.php public function setPasswordAttribute($password) { $this->attributes["password"] = bcrypt($password); }登錄
創(chuàng)建控制器,處理用戶登錄業(yè)務(wù):
$ php artisan make:controller SessionsController
用戶訪問 /login 時,路由分發(fā)該請求:
Route::get("/login","SessionsController@create");
create 方法返回用戶登錄頁面視圖:
/resources/views/sessions/create.blade.php @extends("layouts.master") @section("content")@endsection
用戶點擊登錄后,路由分發(fā)該請求:
Route::post("/login","SessionsController@store");
最后是控制器對登錄行為進行處理:
/app/Http/Controllers/SessionsController.php public function store() { if (!auth()->attempt(request(["email", "password"]))) { return back()->withErrors([ "messages" => "請確保郵箱和密碼正確!" ]); } return redirect()->home(); }
我們使用了 Auth 類提供的 attempt() 進行驗證,只需要傳入 email 和 password 即可,attempt 方法會對密碼經(jīng)過加密后與數(shù)據(jù)庫進行比較,若匹配則
開啟一個通過認證的 session 給用戶。同時,我們還自定義了返回的錯誤信息。
登出的實現(xiàn)比較簡單,首先是路由:
Route::get("/logout","SessionsController@destroy");
控制器:
public function destroy() { auth()->logout(); return redirect()->home(); }
最后,我們優(yōu)化下導(dǎo)航讓,令其根據(jù)用戶登錄信息來顯示不同的設(shè)置項:
注意,如果要讓下拉框生效,需要引入相關(guān)的 js:
權(quán)限控制
實現(xiàn)了登錄與登出功能,就可以對用戶行為進行權(quán)限控制了。
首先是文章的權(quán)限控制,對于「未登錄」的用戶,只可以閱讀文章,因此可以直接使用 Laravel 提供的中間件來實現(xiàn):
/app/Http/Controllers/PostsController.php public function __construct() { $this->middleware("auth")->except(["index","show"]); }
意思是只有授權(quán)的用戶才能夠訪問其他請求,除了 index 與 show 外。
然后是用戶的權(quán)限控制:
/app/Http/Controllers/SessionsController.php public function __construct() { $this->middleware("guest")->except(["destroy"]); }
意思是只有游客才能訪問其他請求,除了 destroy。
完善文章與評論的創(chuàng)建最后,完善文章與評論的創(chuàng)建功能,綁定用戶 id。首先是文章的創(chuàng)建:
/app/Http/Controllers/PostsController.php public function store(Request $request) { $this->validate(request(), [ "title" => "required|unique:posts|max:255", "body" => "required|min:5", ]); $post = new Post(request(["title", "body"])); auth()->user()->publishPost($post); return redirect("posts"); }
創(chuàng)建文章直接使用關(guān)系模型:
/app/User.php public function publishPost(Post $post) { $this->posts()->save($post); }
然后是評論的創(chuàng)建:
public function store(Post $post) { $this->validate(request(),[ "body" => "required|min:5" ]); $post->addComment(new Comment([ "user_id" => auth()->user()->id, "body" => request("body"), ])); return back(); }
同樣使用關(guān)系模型:
/app/Post.php public function addComment(Comment $comment) { $this->comments()->save($comment); }
最后,是一些視圖的更新:
文章列表中,綁定作者:
/resources/views/posts/index.blade.php
具體文章與評論顯示時,也綁定作者:
/resources/views/posts/show.blade.php@foreach ($post->comments as $comment){{ $post->title }}
{{$post->body}}
{{$comment->created_at->diffForHumans() }}{{ $comment->body }}
by {{$comment->user->name }}
@endforeach
Eloquent: 修改器 | Laravel 5.4 中文文檔
Laravel 的用戶認證系統(tǒng) | Laravel 5.4 中文文檔
Navs · Bootstrap
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/22766.html
摘要:我們稍微封裝下代碼吧請您激活賬戶方法用于保存或更新字段方法用于判斷是否在小時之內(nèi)使用管理郵件在剛才的例子中,我們使用的是提供的方法來快速創(chuàng)建和發(fā)送郵件。 使用 Mailtrap 測試郵件功能 Mailtrap 提供了簡單的測試郵件的服務(wù),步驟如下: 登錄網(wǎng)站 Mailtrap 注冊用戶 注冊成功之后,會自動創(chuàng)建一個 demo,點進去之后就可以看到配置信息 showImg(https...
摘要:基本功能創(chuàng)建文章的第一步是用戶發(fā)請求,然后返回創(chuàng)建文章的頁面。實際上,會報錯添加保護雖然我們完成了基本功能,但是提交請求的時候還是會報錯,其實這是防止攻擊。假如違反了規(guī)則,錯誤信息會自動被保存在閃存的中,即只對下一次請求生效。 基本功能 創(chuàng)建文章的第一步是用戶發(fā)請求,然后返回創(chuàng)建文章的頁面。 路由:處理用戶「創(chuàng)建文章」的請求 /routes/web.php Route::get(/po...
摘要:本節(jié)內(nèi)容比較簡單,之前我們使用方法來進行字段驗證,這樣做有一個不好的地方就是,如果你要在很多地方使用同樣的驗證,就需要重復(fù)編寫代碼。因此,提供另外一種方式來進行字段驗證,即請求類。 本節(jié)內(nèi)容比較簡單,之前我們使用 validator 方法來進行字段驗證,這樣做有一個不好的地方就是,如果你要在很多地方使用同樣的驗證,就需要重復(fù)編寫代碼。因此,Laravel 提供另外一種方式來進行字段驗證...
摘要:將上述的一系列查詢進行封裝模型到了這一步,我們基本上實現(xiàn)了文章歸檔的功能。但是有一個問題,文章歸檔實際上包括在通用視圖中,這就意味著,網(wǎng)站的所有請求都需要返回,否則就會報錯。數(shù)據(jù)庫之?dāng)?shù)據(jù)庫請求構(gòu)建器中文文檔的視圖功能中文文檔 首先,要實現(xiàn)的是按照日期來統(tǒng)計文章,原始的 SQL 如下: select year(created_at) year, monthname(c...
摘要:編輯遷移文件我們?yōu)楸砀裉砑恿送怄I,同時生定義了約束,該約束允許刪除父表文章的時候,自動刪除關(guān)聯(lián)的子表評論。關(guān)聯(lián)中文文檔的輔助函數(shù)列表中文文檔 本節(jié)將學(xué)習(xí) Eloquent Relations,表與表之間存在著多種關(guān)系,舉例如下: 一對一:文章與作者 一對多:文章與評論 多對多:標(biāo)簽與文章 文章與評論的一對多關(guān)系 一對多關(guān)系,主要理解兩點: 如何實現(xiàn)一對多關(guān)系 實現(xiàn)了之后能給開發(fā)帶...
閱讀 2576·2023-04-26 02:18
閱讀 1325·2021-10-14 09:43
閱讀 3897·2021-09-26 10:00
閱讀 7372·2021-09-22 15:28
閱讀 2596·2019-08-30 15:54
閱讀 2667·2019-08-30 15:52
閱讀 548·2019-08-29 11:30
閱讀 3525·2019-08-29 11:05