《PHP教程:Laravel搭建后臺(tái)登錄系統(tǒng)步驟詳解》要點(diǎn):
本文介紹了PHP教程:Laravel搭建后臺(tái)登錄系統(tǒng)步驟詳解,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用本文實(shí)例講述了Laravel搭建后臺(tái)登錄系統(tǒng)的方法.分享給大家供大家參考,具體如下:
PHP應(yīng)用今天想用laravel搭建一個(gè)后臺(tái)系統(tǒng),就需要最簡單的那種,有用戶登錄系統(tǒng),試用了下,覺得laravel的用戶登錄這塊做的還真happy.當(dāng)然,前提就是,你要的用戶管理系統(tǒng)是最簡單的那種,就是沒有用戶權(quán)限,能登錄就好.
PHP應(yīng)用我這里就不用默認(rèn)的user表做例子了,那樣很容易和laravel的一些默認(rèn)設(shè)置混淆.
PHP應(yīng)用首先確認(rèn),后臺(tái)的用戶表,我設(shè)計(jì)表叫做badmin,每個(gè)管理員有用戶名(username),有昵稱(nickname),有郵箱(email),有密碼(password)
PHP應(yīng)用這里玩?zhèn)€花,使用laravel的migration來建立表(實(shí)際上可以用不著使用這個(gè)工具建立表)
PHP應(yīng)用1 安裝好最基本的laravel框架
PHP應(yīng)用2 創(chuàng)建migration文件:
PHP應(yīng)用./artisan migrate:make create-badmin-table
PHP應(yīng)用3 發(fā)現(xiàn)app/database/migration/下面多了一個(gè)php文件:
PHP應(yīng)用2014_10_19_090336_create-badmin-table.php
PHP應(yīng)用4 往up和down里面增加內(nèi)容;
PHP應(yīng)用
<?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');
}
}
PHP應(yīng)用5 配置好local的database,app/config/local/database.php
PHP應(yīng)用
<?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',
);
PHP應(yīng)用6 創(chuàng)建數(shù)據(jù)表:
PHP應(yīng)用./artisan migrate --env=local
PHP應(yīng)用這個(gè)時(shí)候去數(shù)據(jù)庫看,就發(fā)現(xiàn)多了一張badmin表,數(shù)據(jù)結(jié)構(gòu)如下:
PHP應(yīng)用
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;
PHP應(yīng)用要問這里為什么多出了create_at和update_at,這是laravel默認(rèn)為每個(gè)表創(chuàng)建的字段,而且在使用Eloquent進(jìn)行增刪改查的時(shí)候能自動(dòng)更新這兩個(gè)字段
PHP應(yīng)用7 創(chuàng)建個(gè)Model:
PHP應(yīng)用
<?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',
];
}
PHP應(yīng)用這里必須要implements UserInterface和RemindableInterface
PHP應(yīng)用8 把model和Auth關(guān)聯(lián)上,修改app/config/auth.php
PHP應(yīng)用
<?php
return array(
// 默認(rèn)的用戶驗(yàn)證驅(qū)動(dòng)
// 可以是database或者eloquent
'driver' => 'eloquent',
// 只有驅(qū)動(dòng)為eloquent的時(shí)候才有用
'model' => 'Badmin',
);
PHP應(yīng)用這里的driver可以是eloquent或者database,使用eloquent就告訴Auth組件說,用戶認(rèn)證類是Badmin這個(gè)類管的.這里的model是有命名空間的,就是說如果你的admin類是\Yejianfeng\Badmin,這里就應(yīng)該改成'\Yejianfeng\Badmin'
PHP應(yīng)用9 好了,這個(gè)時(shí)間其實(shí)邏輯部分已經(jīng)搭建完畢了,你已經(jīng)可以在controller種使用
PHP應(yīng)用Auth::attempt(XXX) 做權(quán)限認(rèn)證
PHP應(yīng)用Auth::user() 獲取登錄用戶(一個(gè)Badmin類)
等.
PHP應(yīng)用10 下面要建立一個(gè)用戶登錄頁面:
PHP應(yīng)用
PHP應(yīng)用11 設(shè)置路由:
PHP應(yīng)用
<?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']);
});
PHP應(yīng)用12 設(shè)置controller:
PHP應(yīng)用
<?php
class UserController extends BaseController {
// 登錄頁面
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' => '注冊失敗'], 410);
}
}
}
PHP應(yīng)用13 設(shè)置下filter,app/filter.php
PHP應(yīng)用
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('/');
}
}
});
PHP應(yīng)用將這里認(rèn)證失敗后的地址轉(zhuǎn)到/ 路徑
PHP應(yīng)用14 設(shè)置views/user/login.blade.php
PHP應(yīng)用這里截取一部分:
PHP應(yīng)用
PHP應(yīng)用可以看出,這里可以直接使用Session::has和Session::get
PHP應(yīng)用然后基本就完成了...
PHP應(yīng)用后記
PHP應(yīng)用laravel這里的auth機(jī)制還是很方便的,但是migration使用起來總覺得有點(diǎn)憋屈.操作數(shù)據(jù)庫總是隔著一層,不爽.
PHP應(yīng)用這里的auth一些簡單的用戶登錄機(jī)制已經(jīng)可以了,但是如果要做更復(fù)雜的用戶管理權(quán)限,估計(jì)要使用Sentry(https://cartalyst.com/manual/sentry)這樣的第三方組件了.
PHP應(yīng)用更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP應(yīng)用希望本文所述對大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/5065.html