laravel ????? ??? ?? ????
May 29, 2023 am 09:58 AMLaravel ?????? PHP ??? ???? ??? ???? ?? ?? ? ?????? ?? ????????. Laravel ?????? ?? ??? ??? ??? ? ?? ???? ??? ????. ? ?? ? ??? ???? ???? ?? ?? ?? ??? ??? ? ??? ??? ?? ??, ???? ???, ??? ??? ?? ??? ?? ??? ??? ? ????. ?. . ?? ???? Laravel ?????? ??? ? ?? ??? ?? ??? ???????.
- ??? ?? ??
?? ???? ?? ??? ??? ??? ???? ???? ???. Laravel?? ???? "make:model" ??? ???? ? ??? ??? ? ????. ???? ?? ??? ?????.
php artisan make:model User -m
? ??? app
????? User
??? ???? ??????? users
? ?????. ???. User
????? ???? ???? ?? ??? ???? ???. Laravel ?????? ??? guard
???? ??? ? ?? ??? ??? ?? ??? ?????: app
目錄下生成一個User
模型,并且在數(shù)據(jù)庫生成一個users
表。在User
模型中,我們需要指定用戶使用的認(rèn)證方式。Laravel框架提供了多種用戶認(rèn)證方式,我們可以在模型中的guard
屬性中進(jìn)行設(shè)置:
protected $guard = 'web';
創(chuàng)建好用戶模型之后,我們需要再創(chuàng)建一個用于展示用戶注冊頁面的控制器,我們使用以下命令來創(chuàng)建該控制器:
php artisan make:controller AuthRegisterController --resource
運行完這個命令之后,Laravel框架將會在appHttpControllersAuth
目錄下創(chuàng)建一個名為RegisterController.php
的文件。在該控制器中,我們需要實現(xiàn)以下方法:
use AppUser; use IlluminateFoundationAuthRegistersUsers; public function register(Request $request) { $this->validator($request->all())->validate(); event(new Registered($user = $this->create($request->all()))); $this->guard()->login($user); return redirect($this->redirectTo); } // 驗證用戶輸入的數(shù)據(jù) protected function validator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); } // 保存注冊用戶信息到數(shù)據(jù)庫 protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), ]); }
接下來,我們需要在routes/web.php
中添加該控制器的路由:
Route::get('register', 'AuthRegisterController@showRegistrationForm')->name('register'); Route::post('register', 'AuthRegisterController@register');
在前臺頁面上,我們使用以下表單來收集用戶數(shù)據(jù):
<form method="POST" action="{{ route('register') }}"> @csrf <div> <label for="name">用戶名</label> <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus> </div> <div> <label for="email">郵箱</label> <input id="email" type="email" name="email" value="{{ old('email') }}" required> </div> <div> <label for="password">密碼</label> <input id="password" type="password" name="password" required> </div> <div> <label for="password-confirm">確認(rèn)密碼</label> <input id="password-confirm" type="password" name="password_confirmation" required> </div> <div> <button type="submit">注冊</button> </div> </form>
然后我們就成功地創(chuàng)建了用戶注冊流程。
- 用戶登錄流程
Laravel提供了非??旖莸挠脩舻卿浄椒ǎ覀兛梢允褂迷摲椒▉磉M(jìn)行用戶登錄。在routes/web.php
中添加以下路由:
Route::get('login', 'AuthLoginController@showLoginForm')->name('login'); Route::post('login', 'AuthLoginController@login'); Route::post('logout', 'AuthLoginController@logout')->name('logout');
然后創(chuàng)建一個與User
模型對應(yīng)的LoginController
控制器。在控制器中,我們需要實現(xiàn)以下方法:
use IlluminateFoundationAuthAuthenticatesUsers; public function login(Request $request) { $this->validateLogin($request); if ($this->attemptLogin($request)) { return $this->sendLoginResponse($request); } return $this->sendFailedLoginResponse($request); } // 認(rèn)證用戶名和密碼 protected function attemptLogin(Request $request) { return $this->guard()->attempt( $this->credentials($request), $request->filled('remember') ); } // 獲取用戶名和密碼 protected function credentials(Request $request) { return $request->only($this->username(), 'password'); } public function username() { return 'email'; } // 用戶退出登錄方法 public function logout(Request $request) { $this->guard()->logout(); $request->session()->invalidate(); return redirect('/'); }
接著,我們需要創(chuàng)建一個用戶登錄視圖。在視圖中,我們使用以下表單來接收用戶數(shù)據(jù):
<form method="POST" action="{{ route('login') }}"> @csrf <div> <label for="email">郵箱</label> <input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus> </div> <div> <label for="password">密碼</label> <input id="password" type="password" name="password" required> </div> <div> <input type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}> <label for="remember">記住我</label> </div> <div> <button type="submit">登錄</button> </div> </form>
在創(chuàng)建好以上視圖和控制器之后,我們就實現(xiàn)了Laravel框架的用戶登錄和注冊流程。需要注意的事項是用戶登錄成功之后,需要將用戶信息存入session中,以便于后續(xù)功能的調(diào)用。在控制器的第二個方法中,我們可以利用Laravel框架提供的attemptLogin
rrreee
rrreee
? ??? ??? ? Laravel ??????appHttpControllersAuth
????? RegisterController.php
?? ??? ?????. ??. ? ??????? ?? ???? ???? ???. ??rrreee?????? routes/web.php
? ? ????? ??? ???? ???. ??rrreee??? ????? ??? ?????. ??? ???? ????? ?? ??? ?????. ??rrreee?? ??? ??? ?? ??? ????? ???????. ??- ????? ??? ??????????Laravel? ??? ???? ??? ? ?? ?? ?? ??? ??? ??? ?????.
routes/web.php
? ?? ??? ?????: ??rrreee?? ?? ?? User
??? ???? LoginController
????? ?????. ?????? ?? ???? ???? ???. ??rrreee?? ???? ??? ??? ?? ???? ???. ???? ?? ??? ???? ??? ???? ?????. ??rrreee???? ?? ????? ?? ? Laravel ?????? ??? ??? ? ?? ????? ??????. ???? ? ?? ???? ????? ???? ? ?? ?? ??? ???? ?? ?? ??? ??? ??? ???? ??? ????. ????? ? ?? ?????? Laravel ??????? ???? attemptLogin
???? ???? ???? ??? ??? ??? ? ????. ??? ???? ??? ??? ??? ???? ???? ?? URL? ???????. ?????????, Laravel ?????? ??? ??? ? ?? ????? ?? ???? ???? ????. ? ??? ?? ??? ??? ? ?? ??? ????? ? ?? ??? ??? ??? ???. ?? ????? ?? ?? ? ?? ??? ??? ?? Laravel ?????? ???? ???? ?? ?? ? ????? ???? ?? ??? ?? ??? ??? ?? ????. ??? ??? laravel ????? ??? ?? ????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

TOWORKEFCITIVE WITHPIVOTTBLESINLARAVEL, FIRDSCESSPIVOTDATAUSINGWITHPIVOT () ORWITHTIMESTAMPS (), thenupdateentrieswithupdatee XistingPivot (), ManagerElationshipsviadetach () andsync (), andusecustompivotmodelswheneded.1.usewithpivot () toincludescificcol

Laravel ?? ???? 4 ?? ?? ??? ?? ?? ???? ???? ???? ? ????. 1. ?? ????? ???? ?? ??? ??? ??? ?? ???? ??? ???? ?????? ??? ??? ????? ?? ??? ?? ???? ??? ??????. 2. ???? ?? ????? ??????? ?????, n 1 ??? ???, ?? ??? ????, ??? ??, ??? ?? ? ?? ? ?? ??, ?? ??? ????. 3. ?? ?? ?? ? ?? ???? ??? ??? ?? ?? ??? ? ??? ???? ???? ???? ???? ???? ? ?? ????? ??????. 4. ??? ?? ? ???? ??? ??? ??? ?? ???? ????? ?? ?????? ????? ?? ???? ? ??? ?? ??? ????? ??????.

Laravelsanctum? SPA ?? ??? ??????? ?? ???? ?? API ??? ??? ?? ??? ?? OAUTH2 ??? ??? ????? ?????. 1. Sanctum? ?? ?? ??? ????, ??? ???? ?????. 2. Passport? ?? ?? ? ????? ?? ??? ?? ??? ????? ????, ?? ???? ?????? ?????. 3. ?? ?? ? ??? ? ???? ?? ?? ??? ????. 4. ?? ??? ?????? ??? ???? ??? ?? ??? ??? ???? ?????. ??? ? ???? ?? ??? ?? OAUTH2 ??? ???? ??? ???????.

Laravel ????? ?????? ??? ???? ???? RefreshDatabase ??, ??? ??? ??, ?? ??? ?? ? ??? ?? ?? ??? ?????. 1. ?? ??? ???? ? ???? ??? ??????? ??????? ???? ?? ?????? ??? ???? ?????????. 2. ?? ??? ???? ??? ???? ??? ?? ???? ?? ?? ???? ?????. 3. DatabasEtransactionStrait? ???? ??? ?? ??? ????? ? ?????????. 4. ???? ???? ???? ??????? ???? ??? ? ?? ? ??????? ?? ??????. ??? ??? ???? ???? ???? ???? ?? ??? ? ??? ??? ?? ???? ?????.

Laravel? ?? ???? ?????? ???? ??? ??????. 1. DB :: Transaction () ???? ???? ??? ???? ???? ?? ?? ?? ?? ?? ??; 2. ?? ? ????? ???? ?? ???? ?? ????? ????? ?? ???? ??? ???? ???? ??? ?? ????. 3.?? ??? ??? ??? ????? ??? begintransaction (), commit () ? rollback ()? ?? ?? ?? ??? ?????. 4. ?? ???? ??? ?? ????, ??? ?? ??, ??? ?? ? ?? ?? ??? ?????. ????? ?? ?? ??? ???? ?? ???? ??? ? ??? ???? ? ????.

Laravel?? HTTP ?? ? ??? ???? ??? ?? ???, ?? ?? ? ?? ???? ??? ????? ????. 1. ?? ???? ?? ? ? ?? ????? ?? ?? ????? ???? input () ?? ?? ???? ???? ??? ?? ??? ?? validate () ?? ?? ?? ???? ?? ? ? ????. 2. Return Response? ???,??, JSON, ?? ?? ? ??? ?? ?? ? ???? ??? ?????. 3. ?? ???? ?? ? ?? ?? () ??? ? Store ()? ???? ??? ???????. ????? ?? ?? ?? ? ??? ?????? ???? ??? ??????? ??? ? ????.

Laravel?? ??? ??? ??? ???? ?? ???? ??? Route () ??? ??? ???? ????. Route () ??? ??? ?? ??? ???? ??? ???? ???? ?? ?? ???? ???? ????. 1. Route ( 'user.profile', [ 'id'=> 1])? ?? ???? ????? ?? ??? ?? ??? ?????. 2. ?? ?? ?? ? ? ?? ? ?????? ??? Route ( 'user.post.show', [ 'id'=> 1, 'postId'=> 10]? ?? ??? ??? ??? ????. 3. ??? ????? ?? ???? ???? ?? ?? ? ? ????. 4. ??? ?? ??? ???? ??? Route ( 'user.post',

Laravel? ??? ?? ??? ?? ???? ?? ?????. ?? ??? ??? ????. 1. ?? ???? ?? ?? ?????. 2. Phpartisanqueue? ?? ???? ??? ? ? ?? ??? ?????? : Work- Queue = High, Default; 3. onqueue () ???? ???? ??? ?? ? ? ? ??? ??????. 4. Laravelhorizon ? ?? ??? ???? ??? ??? ?????? ?????. ?? ?? ?? ?? ?? ? ??? ???? ????? ?? ??? ?? ??? ?? ?????.
