本文實(shí)例講述了laravel搭建后臺(tái)登錄系統(tǒng)的方法。分享給大家供大家參考,具體如下:
今天想用laravel搭建一個(gè)后臺(tái)系統(tǒng),就需要最簡(jiǎn)單的那種,有用戶登錄系統(tǒng),試用了下,覺(jué)得laravel的用戶登錄這塊做的還真happy。當(dāng)然,前提就是,你要的用戶管理系統(tǒng)是最簡(jiǎn)單的那種,就是沒(méi)有用戶權(quán)限,能登錄就好。
我這里就不用默認(rèn)的user表做例子了,那樣很容易和laravel的一些默認(rèn)設(shè)置混淆。
首先確認(rèn),后臺(tái)的用戶表,我設(shè)計(jì)表叫做badmin,每個(gè)管理員有用戶名(username),有昵稱(nickname),有郵箱(email),有密碼(password)
這里玩?zhèn)€花,使用laravel的migration來(lái)建立表(實(shí)際上可以用不著使用這個(gè)工具建立表)
1 安裝好最基本的laravel框架
2 創(chuàng)建migration文件:
./artisan migrate:make create-badmin-table
3 發(fā)現(xiàn)app/database/migration/下面多了一個(gè)php文件:
2014_10_19_090336_create-badmin-table.php
4 往up和down里面增加內(nèi)容;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php use illuminate\database\schema\blueprint; use illuminate\database\migrations\migration; class createbadmintable extends migration { /** * run the migrations. * * @return void */ public function up() { schema::create( 'badmin' , function ( $table ) { $table ->increments( 'id' ); $table ->string( 'nickname' , 100)->unique(); $table ->string( 'username' , 100)->unique(); $table ->string( 'email' , 100)->unique(); $table ->string( 'password' , 64); $table ->timestamps(); }); } /** * reverse the migrations. * * @return void */ public function down() { schema::drop( 'badmin' ); } } |
5 配置好local的database,app/config/local/database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php return array ( 'fetch' => pdo::fetch_class, 'default' => 'mysql' , 'connections' => array ( 'mysql' => array ( 'driver' => 'mysql' , 'host' => 'localhost' , 'database' => 'test' , 'username' => 'yejianfeng' , 'password' => '123456' , 'charset' => 'utf8' , 'collation' => 'utf8_unicode_ci' , 'prefix' => '' , ), ), 'migrations' => 'migrations' , ); |
6 創(chuàng)建數(shù)據(jù)表:
./artisan migrate --env=local
這個(gè)時(shí)候去數(shù)據(jù)庫(kù)看,就發(fā)現(xiàn)多了一張badmin表,數(shù)據(jù)結(jié)構(gòu)如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
create table `badmin` ( `id` int(10) unsigned not null auto_increment, `nickname` varchar(100) collate utf8_unicode_ci not null, `username` varchar(100) collate utf8_unicode_ci not null, `email` varchar(100) collate utf8_unicode_ci not null, `password` varchar(64) collate utf8_unicode_ci not null, `created_at` timestamp not null default '0000-00-00 00:00:00' , `updated_at` timestamp not null default '0000-00-00 00:00:00' , primary key (`id`), unique key `badmin_nickname_unique` (`nickname`), unique key `badmin_username_unique` (`username`), unique key `badmin_email_unique` (`email`) ) engine=innodb auto_increment=2 default charset=utf8 collate=utf8_unicode_ci; |
要問(wèn)這里為什么多出了create_at和update_at,這是laravel默認(rèn)為每個(gè)表創(chuàng)建的字段,而且在使用eloquent進(jìn)行增刪改查的時(shí)候能自動(dòng)更新這兩個(gè)字段
7 創(chuàng)建個(gè)model:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php use illuminate\auth\usertrait; use illuminate\auth\userinterface; use illuminate\auth\reminders\remindabletrait; use illuminate\auth\reminders\remindableinterface; class badmin extends eloquent implements userinterface, remindableinterface { use usertrait, remindabletrait; protected $table = 'badmin' ; protected $hidden = array ( 'password' ); public static $rules = [ 'nickname' => 'required|alpha_num|min:2' , 'username' => 'required' , 'email' => 'required|email|unique:badmin' , 'password' => 'required|alpha_num|between:6,12|confirmed' , ]; } |
這里必須要implements userinterface和remindableinterface
8 把model和auth關(guān)聯(lián)上,修改app/config/auth.php
1
2
3
4
5
6
7
8
|
<?php return array ( // 默認(rèn)的用戶驗(yàn)證驅(qū)動(dòng) // 可以是database或者eloquent 'driver' => 'eloquent' , // 只有驅(qū)動(dòng)為eloquent的時(shí)候才有用 'model' => 'badmin' , ); |
這里的driver可以是eloquent或者database,使用eloquent就告訴auth組件說(shuō),用戶認(rèn)證類是badmin這個(gè)類管的。這里的model是有命名空間的,就是說(shuō)如果你的admin類是\yejianfeng\badmin,這里就應(yīng)該改成'\yejianfeng\badmin'
9 好了,這個(gè)時(shí)間其實(shí)邏輯部分已經(jīng)搭建完畢了,你已經(jīng)可以在controller種使用
auth::attempt(xxx) 做權(quán)限認(rèn)證
auth::user() 獲取登錄用戶(一個(gè)badmin類)
等。
10 下面要建立一個(gè)用戶登錄頁(yè)面:
11 設(shè)置路由:
1
2
3
4
5
6
7
8
9
10
11
|
<?php // 不需要登錄驗(yàn)證的接口 route::get( '/' , [ 'as' => 'user.login' , 'uses' => 'usercontroller@getlogin' ]); route::get( 'user/login' , [ 'as' => 'login' , 'uses' => 'usercontroller@getlogin' ]); route::post( 'user/login' , [ 'as' => 'login' , 'uses' => 'usercontroller@postlogin' ]); // 需要登錄驗(yàn)證才能操作的接口 route::group( array ( 'before' => 'auth' ), function () { route::get( 'user/logout' , [ 'as' => 'logout' , 'uses' => 'usercontroller@getlogout' ]); route::get( 'user/dashboard' , [ 'as' => 'dashboard' , 'uses' => 'usercontroller@getdashboard' ]); }); |
12 設(shè)置controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?php class usercontroller extends basecontroller { // 登錄頁(yè)面 public function getlogin() { return view::make( 'user.login' ); } // 登錄操作 public function postlogin() { if (auth::attempt( array ( 'email' =>input::get( 'email' ), 'password' =>input::get( 'password' )))) { return redirect::to( 'user/dashboard' ) ->with( 'message' , '成功登錄' ); } else { return redirect::to( 'user/login' ) ->with( 'message' , '用戶名密碼不正確' ) ->withinput(); } } // 登出 public function getlogout() { auth::logout(); return redirect::to( 'user/login' ); } public function getdashboard() { return view::make( 'user.dashboard' ); } // 添加新用戶操作 public function getcreate() { return view::make( 'user.create' ); } // 添加新用戶操作 public function postcreate() { $validator = validator::make(input::all(), user:: $rules ); if ( $validator ->passes()){ $badmin = new badmin(); $badmin ->nickname = input::get( 'nickname' ); $badmin ->username = input::get( 'username' ); $badmin ->email = input::get( 'email' ); $user ->password = hash::make(input::get( 'password' )); $user ->save(); response::json(null); } else { response::json([ 'message' => '注冊(cè)失敗' ], 410); } } } |
13 設(shè)置下filter,app/filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
route::filter( 'auth' , function () { if (auth::guest()) { if (request::ajax()) { return response::make( 'unauthorized' , 401); } else { return redirect::guest( '/' ); } } }); |
將這里認(rèn)證失敗后的地址轉(zhuǎn)到/ 路徑
14 設(shè)置views/user/login.blade.php
這里截取一部分:
可以看出,這里可以直接使用session::has和session::get
然后基本就完成了...
后記
laravel這里的auth機(jī)制還是很方便的,但是migration使用起來(lái)總覺(jué)得有點(diǎn)憋屈。操作數(shù)據(jù)庫(kù)總是隔著一層,不爽。
這里的auth一些簡(jiǎn)單的用戶登錄機(jī)制已經(jīng)可以了,但是如果要做更復(fù)雜的用戶管理權(quán)限,估計(jì)要使用sentry()這樣的第三方組件了。
希望本文所述對(duì)大家基于laravel框架的php程序設(shè)計(jì)有所幫助。