Laravel搭建后臺(tái)登錄系統(tǒng)步驟詳解
本文實(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)容;
<?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
<?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)如下:
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:
<?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
<?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è)置路由:
<?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:
<?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
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(https://cartalyst.com/manual/sentry)這樣的第三方組件了。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門(mén)與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP中Session可能會(huì)引起并發(fā)問(wèn)題
Session 中文沒(méi)有一個(gè)統(tǒng)一的譯法,我習(xí)慣上譯為會(huì)話。關(guān)于session的意義大家都應(yīng)該清楚: 其實(shí)是在瀏覽某個(gè)網(wǎng)站時(shí),在瀏覽器沒(méi)有關(guān)閉的情形之下,一個(gè)web應(yīng)用的開(kāi)始和結(jié)束。一個(gè)session可以包括數(shù)次http的請(qǐng)求和應(yīng)答2015-06-06
laravel 字段格式化 modle 字段類型轉(zhuǎn)換方法
今天小編就為大家分享一篇laravel 字段格式化 modle 字段類型轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
Thinkphp5.1獲取項(xiàng)目根目錄以及子目錄路徑的方法實(shí)例講解
這篇文章主要介紹了Thinkphp5.1獲取項(xiàng)目根目錄以及子目錄路徑的方法實(shí)例講解,希望正在學(xué)習(xí)TP框架的同學(xué)可以跟著小編一起來(lái)學(xué)習(xí)研究下2021-03-03
php 使用html5實(shí)現(xiàn)多文件上傳實(shí)例
在html沒(méi)有出來(lái)之前,要實(shí)現(xiàn)php多文件上傳比較麻煩,需要在form表單里面添加多個(gè)input file域。html5發(fā)布以后,我們可以使用input file的html5屬性multiple來(lái)實(shí)現(xiàn)多文件上傳,需要的朋友可以參考下2016-10-10
laravel 錯(cuò)誤處理,接口錯(cuò)誤返回json代碼
今天小編就為大家分享一篇laravel 錯(cuò)誤處理,接口錯(cuò)誤返回json代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Yii2框架可逆加密簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Yii2框架可逆加密簡(jiǎn)單實(shí)現(xiàn)方法,涉及Yii框架encryptByPassword()與decryptByPassword()函數(shù)簡(jiǎn)單使用方法,需要的朋友可以參考下2017-08-08

