laravel用戶登錄注冊

users

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255)  NOT NULL,
  `email` varchar(255)  NOT NULL,
  `remember_token` varchar(100)  DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_name_unique` (`name`),
  UNIQUE KEY `users_email_unique` (`email`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

admins

CREATE TABLE `admins` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255)  NOT NULL,
  `email` varchar(255)  NOT NULL,
  `remember_token` varchar(100)  DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_name_unique` (`name`),
  UNIQUE KEY `users_email_unique` (`email`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

模型

User(model)前臺

<?php
namespace App\Model; //修改了命名空間
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends  Authenticatable
{ 
} 

Admin(model)后臺

<?php
namespace App\Model; 
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends  Authenticatable
{ 
} 

修改config/auth.php

guards' => [
         //前臺
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        //后臺
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
      ...
'providers' => [
        'users' => [   //前臺
            'driver' => 'eloquent',
            'model' => \App\Model\User::class,
        ],
        'admins' => [ //后臺
            'driver' => 'eloquent',
            'model' => \App\Model\Admin::class,
        ],
      ...

路由

前臺

Route::group(['middleware'=>'auth:web'],function(){
    Route::get('/home','HomeController@index');
});

后臺

Route::group(['middleware'=>'auth:admin'],function(){
    Route::get('/admin','AdminController@index');
});

控制器

只列舉了前臺行為

注冊行為
public function register(){
 //驗證
 $this->validate(\request(),[
     'name' => 'required|min:2|unique:users,name',
     'email' => 'required|unique:users,email|email',
     'password' => 'required|min:5|confirmed',
   ]);
 $password = bcrypt(request('password'));
 $name = request('name');
 $email = request('email');
 $user = User::create(compact('name', 'email', 'password'));
 return redirect('/login');
}
登陸行為
public function login(Request $request){
     $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required|min:6|max:30',
        'is_remember' => '',
     ]);
  $user = request(['email', 'password']);
  $remember = boolval(request('is_remember'));
    if (true == Auth::attempt($user, $remember)) {
          return redirect('/posts');
   }
   return Redirect::back()->withErrors("用戶名密碼錯誤");
}
登出
    public function logout()
    {
        Auth::logout();
        return redirect('/login');
    }

驗證信息本地化

復制resources/lang/en為zh,改'locale'=>'zh',下面時zh的validation.php

<?php

return [

    'unique'               => ':attribute 已存在',
    'accepted'             => ':attribute 是被接受的',
    'active_url'           => ':attribute 必須是一個合法的 URL',
    'after'                => ':attribute 必須是 :date 之后的一個日期',
    'alpha'                => ':attribute 必須全部由字母字符構成。',
    'alpha_dash'           => ':attribute 必須全部由字母、數(shù)字、中劃線或下劃線字符構成',
    'alpha_num'            => ':attribute 必須全部由字母和數(shù)字構成',
    'array'                => ':attribute 必須是個數(shù)組',
    'before'               => ':attribute 必須是 :date 之前的一個日期',
    'between'              => [
        'numeric' => ':attribute 必須在 :min 到 :max 之間',
        'file'    => ':attribute 必須在 :min 到 :max KB之間',
        'string'  => ':attribute 必須在 :min 到 :max 個字符之間',
        'array'   => ':attribute 必須在 :min 到 :max 項之間',
    ],
    'boolean'              => ':attribute 字符必須是 true 或 false',
    'confirmed'            => ':attribute 二次確認不匹配',
    'date'                 => ':attribute 必須是一個合法的日期',
    'date_format'          => ':attribute 與給定的格式 :format 不符合',
    'different'            => ':attribute 必須不同于:other',
    'digits'               => ':attribute 必須是 :digits 位',
    'digits_between'       => ':attribute 必須在 :min and :max 位之間',
    'email'                => ':attribute 必須是一個合法的電子郵件地址。',
    'filled'               => ':attribute 的字段是必填的',
    'exists'               => '選定的 :attribute 是無效的',
    'image'                => ':attribute 必須是一個圖片 (jpeg, png, bmp 或者 gif)',
    'in'                   => '選定的 :attribute 是無效的',
    'integer'              => ':attribute 必須是個整數(shù)',
    'ip'                   => ':attribute 必須是一個合法的 IP 地址。',
    'max'                  => [
        'numeric' => ':attribute 的最大長度為 :max 位',
        'file'    => ':attribute 的最大為 :max',
        'string'  => ':attribute 的最大長度為 :max 字符',
        'array'   => ':attribute 的最大個數(shù)為 :max 個',
    ],
    'mimes'                => ':attribute 的文件類型必須是:values',
    'min'                  => [
        'numeric' => ':attribute 的最小長度為 :min 位',
        'string'  => ':attribute 的最小長度為 :min 字符',
        'file'    => ':attribute 大小至少為:min KB',
        'array'   => ':attribute 至少有 :min 項',
    ],
    'not_in'               => '選定的 :attribute 是無效的',
    'numeric'              => ':attribute 必須是數(shù)字',
    'regex'                => ':attribute 格式是無效的',
    'required'             => ':attribute 字段必須填寫',
    'required_if'          => ':attribute 字段是必須的當 :other 是 :value',
    'required_with'        => ':attribute 字段是必須的當 :values 是存在的',
    'required_with_all'    => ':attribute 字段是必須的當 :values 是存在的',
    'required_without'     => ':attribute 字段是必須的當 :values 是不存在的',
    'required_without_all' => ':attribute 字段是必須的當 沒有一個 :values 是存在的',
    'same'                 => ':attribute 和 :other 必須匹配',
    'size'                 => [
        'numeric' => ':attribute 必須是 :size 位',
        'file'    => ':attribute 必須是 :size KB',
        'string'  => ':attribute 必須是 :size 個字符',
        'array'   => ':attribute 必須包括 :size 項',
    ],
    'url'                  => ':attribute 無效的格式',
    'timezone'             => ':attribute 必須個有效的時區(qū)',

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | Here you may specify custom validation messages for attributes using the
    | convention "attribute.rule" to name the lines. This makes it quick to
    | specify a specific custom language line for a given attribute rule.
    |
    */
    'custom'               => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap attribute place-holders
    | with something more reader friendly such as E-Mail Address instead
    | of "email". This simply helps us make messages a little cleaner.
    |
    */
    'attributes'           => [
        'username' => '用戶名',
        'account'  => '賬號',
        'captcha'  => '驗證碼',
        'mobile'   => '手機號',
        'password' => '密碼',
        'content'  => '內容',
        'comment'  => '評論',
        'identity' => '手機號/用戶名',
        'title' => '標題',
        'introduction'  =>  '簡介',
    ],

];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 轉載自cr180大神DiscuzX2.5完整目錄結構【source程序文件庫】 /source/admincp后臺...
    cndaqiang閱讀 884評論 1 2
  • 文章分類 后臺文章分類列表頁模板導的詳細步驟建立數(shù)據(jù)表blog_category,并添加相應的文章字段使用php ...
    JoyceZhao閱讀 1,764評論 0 14
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,937評論 18 139
  • 簡單說的很對,一廂情愿就要愿賭服輸。昨天是七夕,一個和我依舊沒有多大關系的節(jié)日,隨手刷屏,看到了他。他@了一個...
    果子大大王閱讀 351評論 0 2
  • 風卷 葉落 離開梢頭的綠色 跌入 滑下 陷進淺淺的沼澤 僵硬的身軀 殘破 無力的撫著枯黃的脈絡 昂首 ...
    墨淺封閱讀 122評論 0 0