亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 64
粉絲 6
評(píng)論 2
訪問量 100628
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
laravel--新建模型--實(shí)現(xiàn)前臺(tái)用戶登錄驗(yàn)證
王嬌
原創(chuàng)
1470人瀏覽過

學(xué)習(xí)總結(jié)

1.新建模型后,如果需要使用Auth類進(jìn)行登錄驗(yàn)證,需要在config文件夾中的auth.php文件進(jìn)行門衛(wèi)注冊(cè)
2.在使用Auth類進(jìn)行登錄驗(yàn)證時(shí),需要使用自定義的門衛(wèi)進(jìn)行驗(yàn)證$isLogin = Auth::guard('member')->attempt(['phone'=>$phone,'password'=>$password])

1.新建一個(gè)模型Member.php

在app目錄下新建一個(gè)member.php的模型文件

  1. <?php
  2. namespace App;
  3. use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Foundation\Auth\User as Authenticatable;
  5. use Illuminate\Notifications\Notifiable;
  6. class Member extends Authenticatable
  7. {
  8. use Notifiable;
  9. //指定手動(dòng)用戶驗(yàn)證的表名
  10. protected $table = 'member';
  11. /**
  12. * The attributes that are mass assignable.
  13. *
  14. * @var array
  15. */
  16. protected $fillable = [
  17. 'name', 'email', 'password',
  18. ];
  19. /**
  20. * The attributes that should be hidden for arrays.
  21. *
  22. * @var array
  23. */
  24. protected $hidden = [
  25. 'password', 'remember_token',
  26. ];
  27. /**
  28. * The attributes that should be cast to native types.
  29. *
  30. * @var array
  31. */
  32. protected $casts = [
  33. 'email_verified_at' => 'datetime',
  34. ];
  35. }

2.在config文件夾中的auth.php中注冊(cè)新建的模型文件

  1. <?php
  2. return [
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Authentication Defaults
  6. |--------------------------------------------------------------------------
  7. |
  8. | This option controls the default authentication "guard" and password
  9. | reset options for your application. You may change these defaults
  10. | as required, but they're a perfect start for most applications.
  11. |
  12. */
  13. 'defaults' => [
  14. 'guard' => 'web',
  15. 'passwords' => 'users',
  16. ],
  17. /*
  18. |--------------------------------------------------------------------------
  19. | Authentication Guards
  20. |--------------------------------------------------------------------------
  21. |
  22. | Next, you may define every authentication guard for your application.
  23. | Of course, a great default configuration has been defined for you
  24. | here which uses session storage and the Eloquent user provider.
  25. |
  26. | All authentication drivers have a user provider. This defines how the
  27. | users are actually retrieved out of your database or other storage
  28. | mechanisms used by this application to persist your user's data.
  29. |
  30. | Supported: "session", "token"
  31. |
  32. */
  33. 'guards' => [
  34. 'web' => [
  35. 'driver' => 'session',
  36. 'provider' => 'users',
  37. ],
  38. 'api' => [
  39. 'driver' => 'token',
  40. 'provider' => 'users',
  41. 'hash' => false,
  42. ],
  43. //自定義guards為member
  44. 'member' => [
  45. 'driver' => 'session',
  46. 'provider' => 'members',
  47. ],
  48. ],
  49. /*
  50. |--------------------------------------------------------------------------
  51. | User Providers
  52. |--------------------------------------------------------------------------
  53. |
  54. | All authentication drivers have a user provider. This defines how the
  55. | users are actually retrieved out of your database or other storage
  56. | mechanisms used by this application to persist your user's data.
  57. |
  58. | If you have multiple user tables or models you may configure multiple
  59. | sources which represent each model / table. These sources may then
  60. | be assigned to any extra authentication guards you have defined.
  61. |
  62. | Supported: "database", "eloquent"
  63. |
  64. */
  65. 'providers' => [
  66. 'users' => [
  67. 'driver' => 'eloquent',
  68. 'model' => App\User::class,
  69. ],
  70. //自定義guard為member ,使用模型為Member
  71. 'members' => [
  72. 'driver' => 'eloquent',
  73. 'model' => App\Member::class,
  74. ],
  75. // 'users' => [
  76. // 'driver' => 'database',
  77. // 'table' => 'users',
  78. // ],
  79. ],
  80. /*
  81. |--------------------------------------------------------------------------
  82. | Resetting Passwords
  83. |--------------------------------------------------------------------------
  84. |
  85. | You may specify multiple password reset configurations if you have more
  86. | than one user table or model in the application and you want to have
  87. | separate password reset settings based on the specific user types.
  88. |
  89. | The expire time is the number of minutes that the reset token should be
  90. | considered valid. This security feature keeps tokens short-lived so
  91. | they have less time to be guessed. You may change this as needed.
  92. |
  93. */
  94. 'passwords' => [
  95. 'users' => [
  96. 'provider' => 'users',
  97. 'table' => 'password_resets',
  98. 'expire' => 60,
  99. 'throttle' => 60,
  100. ],
  101. ],
  102. /*
  103. |--------------------------------------------------------------------------
  104. | Password Confirmation Timeout
  105. |--------------------------------------------------------------------------
  106. |
  107. | Here you may define the amount of seconds before a password confirmation
  108. | times out and the user is prompted to re-enter their password via the
  109. | confirmation screen. By default, the timeout lasts for three hours.
  110. |
  111. */
  112. 'password_timeout' => 10800,
  113. ];

3.在middle文件夾中新建一個(gè)AuthMember.php的中間件

  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Hamcrest\Arrays\IsArray;
  5. //引入數(shù)據(jù)庫查詢構(gòu)造器,鏈?zhǔn)秸{(diào)用
  6. use Illuminate\Support\Facades\DB;
  7. //引入Auth類,獲取當(dāng)前登錄的用戶
  8. use Illuminate\Support\Facades\Auth;
  9. use function GuzzleHttp\json_decode;
  10. class AuthMember
  11. {
  12. /**
  13. * Handle an incoming request.
  14. *
  15. * @param \Illuminate\Http\Request $request
  16. * @param \Closure $next
  17. * @return mixed
  18. */
  19. public function handle($request, Closure $next)
  20. {
  21. //如果用戶沒有登錄Auth::guard('member')->guest()返回真
  22. if(Auth::guard('member')->guest())
  23. {
  24. if($request->ajax())
  25. {
  26. $res = ['code'=>'401','msg'=>'no login'];
  27. return response(json_encode($res),200);
  28. }
  29. return redirect()->guest('/homes/account/login');
  30. }
  31. return $next($request);
  32. }
  33. }

4.在kernel.php中注冊(cè)前臺(tái)用戶登錄驗(yàn)證中間件

  1. <?php
  2. namespace App\Http;
  3. use Illuminate\Foundation\Http\Kernel as HttpKernel;
  4. class Kernel extends HttpKernel
  5. {
  6. /**
  7. * The application's global HTTP middleware stack.
  8. *
  9. * These middleware are run during every request to your application.
  10. *
  11. * @var array
  12. */
  13. protected $middleware = [
  14. // \App\Http\Middleware\TrustHosts::class,
  15. \App\Http\Middleware\TrustProxies::class,
  16. \Fruitcake\Cors\HandleCors::class,
  17. \App\Http\Middleware\CheckForMaintenanceMode::class,
  18. \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
  19. \App\Http\Middleware\TrimStrings::class,
  20. \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
  21. ];
  22. /**
  23. * The application's route middleware groups.
  24. *
  25. * @var array
  26. */
  27. protected $middlewareGroups = [
  28. 'web' => [
  29. \App\Http\Middleware\EncryptCookies::class,
  30. \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
  31. \Illuminate\Session\Middleware\StartSession::class,
  32. // \Illuminate\Session\Middleware\AuthenticateSession::class,
  33. \Illuminate\View\Middleware\ShareErrorsFromSession::class,
  34. \App\Http\Middleware\VerifyCsrfToken::class,
  35. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  36. ],
  37. 'api' => [
  38. 'throttle:60,1',
  39. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  40. ],
  41. ];
  42. /**
  43. * The application's route middleware.
  44. *
  45. * These middleware may be assigned to groups or used individually.
  46. *
  47. * @var array
  48. */
  49. protected $routeMiddleware = [
  50. 'auth' => \App\Http\Middleware\Authenticate::class,
  51. 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
  52. 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
  53. 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
  54. 'can' => \Illuminate\Auth\Middleware\Authorize::class,
  55. 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
  56. 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
  57. 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
  58. 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
  59. 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
  60. //注冊(cè)權(quán)限控制中間件
  61. 'rights' => \App\Http\Middleware\RightsVerify::class,
  62. //用戶登錄驗(yàn)證中間件
  63. 'member' => \App\Http\Middleware\AuthMember::class,
  64. ];
  65. }

5.使用member模型進(jìn)行前臺(tái)用戶登錄驗(yàn)證

本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)