1. Ê×ÏÈȶ¨Óû§×¢²aµÄÂÓÉ
ÎÒÃÇÔÚ°²×°ºÃlaravelµÄʱºoĬÈÏÉu³ÉµÄ×¢²aÊÇÓÃÓÊÏa½øÐÐ×¢²aµÄ£¬²¢ÇÒÓÐÐ(C)Ñ¡Ïi²»ÐeÒª£¬ÓÐÐ(C)»¹ÐeÒª¼ÓÒ»Ð(C)±iµ¥Ñ¡Ïi
ÎÒÃÇ×¢²aµÄ»°£¬²¢²»ÊÇ¿ÉÒÔËae±a×¢²aµÄ£¬Ö»ÓÐÒ»Ð(C)³¬¼¶¹ÜÀiÔ±²ÅÄܽøÐÐ×¢²a
Ê×ÏÈÎÒÃÇʹÓÃÉϴδ´½¨µÄUserController
½øÐÐÅaÖã¬Èç¹uûÓеĻ°£¬¿ÉÒÔʹÓÃphp artisan make:controller UserController
´´½¨Ò»¸o¿ØÖÆÆ÷Àa
È»ºo´´½¨Á½ÌoÂ*ÓÉRoute::get('register', 'UserController@getRegister')
ºÍRoute::post('register', 'UserController@postRegister')
Ç°ÕßÊÇÏÔʾһ¸o×¢²aµÄÒ³Ãaeget
ÇeÇo£¬ºoÃaeÊÇ×¢²aÕ˺ŵÄpost
ÇeÇo¡£
2. ÏÔʾע²aÕ˺ÅÒ³Ãae
Õa¸oʹÓõÄÊÇgetRegister
Õa¸o½¨£¬Õa¸o½¨Ö»ÐeÒªÏÔʾһ¸oÊÓͼËuÒÔ²¢Ã»ÓÐÌرðµÄÂß¼
public function getRegister()
{
return view('auth.register');
}
3. ÇeÇo×¢²aÕ˺Å
Õa¸oʹÓõÄÊÇpostRegister
Õa¸o½¨
×¢²aÕ˺ŵĻ°ºÍÖØÖÃÃÜÂeÒ»Ñu£¬¶øÇÒ±È×¢²aÕ˺Ż¹Òª¼oµ¥µa¡£
ÎÒÃÇÔÚÍuÊý¾Ý¿aÀi²aÈeÒ»ÌoÓû§¼Í¼µÄʱºo£¬¿ÉÒÔʹÓÃUser::create($data)
½øÐвaÈe¡£
$data
ÊǸoÊý×e£¬ÀiÃae´ae*ÅÁËÿ¸o×ֶεļuºÍÖµ
public function postRegister(Request $request)
{
$rules = [
'username'=>'required|unique:finance_enewsuser',
'password' => 'required|between:6,20|confirmed'
];
$messages = [
'required'=>':attribute²»ÄÜΪ¿Õ',
'unique'=>'Óû§ÃuÒѱ»×¢²a',
'between' => 'ÃÜÂe±ØÐeÊÇ6~20λ֮¼a',
'confirmed' => 'ÐÂÃÜÂeºÍÈ*ÈÏÃÜÂe²»Æ¥Åa'
];
$username = $request->input('username');
$password = $request->input('password');
$group = $request->input('group');
$data = $request->all();
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
return back()->withErrors($validator);
}
$data = [
'username' => $username,
'password' => bcrypt($password),
'groupid' => $group,
'checked' => 0,
'styleid' => 1,
'filelevel' => 0,
'loginnum' => 0,
'lasttime' => time(),
'lastip' => '127.0.0.1',
'truename' => '',
'email' => '',
'pretime' => time(),
'preip' => '127.0.0.1',
];
User::create($data); //²aÈeÒ»Ìoмͼ£¬²¢*µ»Ø±£´aeºoµÄÄ£ÐÍʵÀý
//Èç¹u×¢²aºo»¹ÏeÁ¢¼´µÇ¼µÄ»°£¬¿ÉÒÔʹÓÃ$user = User::create($data); Auth::login($user); ½øÐÐÈÏÖ¤
return redirect('/');
}
4. Íe³ÉºoµÄʾÀý
UserController
public function getRegister()
{
return view('auth.register');
}
public function postRegister(Request $request)
{
$rules = [
'username'=>'required|unique:finance_enewsuser',
'password' => 'required|between:6,20|confirmed'
];
$messages = [
'required'=>':attribute²»ÄÜΪ¿Õ',
'unique'=>'Óû§ÃuÒѱ»×¢²a',
'between' => 'ÃÜÂe±ØÐeÊÇ6~20λ֮¼a',
'confirmed' => 'ÐÂÃÜÂeºÍÈ*ÈÏÃÜÂe²»Æ¥Åa'
];
$username = $request->input('username');
$password = $request->input('password');
$group = $request->input('group');
$data = $request->all();
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
return back()->withErrors($validator);
}
$data = [
'username' => $username,
'password' => bcrypt($password),
'groupid' => $group,
'checked' => 0,
'styleid' => 1,
'filelevel' => 0,
'loginnum' => 0,
'lasttime' => time(),
'lastip' => '127.0.0.1',
'truename' => '',
'email' => '',
'pretime' => time(),
'preip' => '127.0.0.1',
];
User::create($data); //²aÈeÒ»Ìoмͼ£¬²¢*µ»Ø±£´aeºoµÄÄ£ÐÍʵÀý
return redirect('/');
}
register.blade
<form class="login-form" action="" method="post">
{!! csrf_field() !!}
<h3 class="font-green">Sign Up</h3>
@if(count($errors) > 0)
<div class="alert alert-danger display-hide" style="display: block;">
<button class="close" data-close="alert"></button>
<span> </span>
</div>
@endif
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Óû§Ãu</label>
<input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username"> </div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">ÃÜÂe</label>
<input class="form-control placeholder-no-fix" type="password" autocomplete="off" id="register_password" placeholder="Password" name="password"> </div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Öظ´ÃÜÂe</label>
<input class="form-control placeholder-no-fix" type="password" autocomplete="off" placeholder="Repeat password" name="password_confirmation"> </div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Óû§×e</label>
<select name="group" class="form-control">
<option value="1"> ³¬¼¶¹ÜÀiÔ± </option>
<option value="2"> ¹ÜÀiÔ± </option>
<option value="3"> ±a¼ </option>
</select>
</div>
<div class="form-actions">
<button type="submit" id="register-submit-btn" class="btn btn-success uppercase pull-right">×¢²a</button>
</div>
</form>
5. Öмa¼þ¨CÓû§±ØÐeµÇ¼
ÏÖÔÚ×¢²a¶¼Íe³ÉÁË,ÎÒÃǾͲiÓû§µÄÅжÏÁË¡£ ÐeÇo×¢²aÕ˺űØÐeÖ»ÄÜÊÇÓг¬¼¶¹ÜÀiԱȨÏÞµÄÕ˺ŲſÉÒÔ×¢²a¡£
ÕaÖÖÇe¿oÏ°´ÕÕÎÒÃÇÒ»°aµÄ²½Öe¾ÍÊÇÔÚpostRegister½¨ÀiÖ±½Ó²e³oÓû§µÄÐÅÏ¢£¬È»ºo²e¿´Óû§ÊÇ*ñÂu×aÕa¸oȨÏÞ£¬²»Âu×aµÄÇe¿oϾÍÌøתµ½ÆaËuÒ³Ãae¡£
ÕaÖÖ½¨¿ÉÒÔ£¬µ«ÊÇ£¬ÎÒÃǼÈÈ»Óг¬¼¶¹ÜÀiÔ±ºÍ¹ÜÀiÔ±ÕaÐ(C)ȨÏÞÇøÖ£¬¿Ï¶¨²»Ö¹Ò»¸oµØ½Ê¹Óã¬ÆaËuµØ*½Ò²»aÓõ½¡£
È»ºo»aÓÐÈËÏeµ½ÔÚmodel
Àiд¸o½¨£¬ÒÔºoÓÐÐeÒª¶¼¿ÉÒÔÖ±½Óµ÷Óá£
Õa¸o½¨Ò²¿ÉÒÔ£¬²»¹ý£¬ÎÒÃÇÍƼoʹÓÃlaravelÌa¹(C)µÄÖмa¼þÕa¸o¹¦ÄÜ£¬Õa¸o¹¦ÄÜdz£Ç¿´o£¬Ò²Ç³£ºÃÓá£ÏÖÔÚÎÒÃǾÍʹÓÃÖмa¼þÕa¸o¹¦ÄÜ¡£
ÒoΪÎÒÃÇÊǺǫÄÚÈݹÜÀiϵͳ£¬ËuÒÔ£¬ÎÒÃÇÊ×ÏÈ´´½¨Ò»¸oÖмa¼þ£¬¹¦ÄÜÊÇ£¬ËuÓÐÒ³Ãae½øÈeÇ°£¬±ØÐeÊǵǼ״̬£¬*ñÔoÌøµ½µÇ¼ҳ¡£
²e¿´ÊÖ²a*¢ÏÖ¿ÉÒÔʹÓÃphp artisan make:middleware CheckLoginMiddleware
ÃuÁi´´½¨Ò»¸oÖмa¼þ£¬µ±È»¸´ÖÆÒ»¸o²i²»¶aµÄÎļþ£¬¸ÄÏÂÒ²ÊÇÒ»ÑuµÄ¡£
È»ºo»aÔÚapp/Http/Middleware/
Ŀ¼Ï´´½¨ÁËÒ»¸oCheckLoginMiddleware
Öмa¼þÎļþ£¬ÀiÃaeÖ»ÓÐÒ»¸ohandle()
½¨£¬ÎÒÃÇÖ±½ÓÔÚÀiÃaeÔo¼ÓÎÒÃǵŦÄÜ
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class CheckLoginMiddleware
{
public function handle($request, Closure $next)
{
//ʹÓÃAuth*½*¨£¬ÐeÒªÒýÈeuse Auth;*½*¨
//$request->is('login')±iʾÇeÇoµÄURLÊÇ*ñÊǵǼҳ
//ÒoΪÎÒÃÇ´oËaʹÓÃÈ«¾ÖµÄ£¬ËuÒÔ£¬ÐeÒª°ÑµÇ¼ҳÅųý£¬²»È»»aÎÞÏÞÖض¨Ïo
//Èç¹uÄaµÄµÇ¼ҳ²»ÊÇ/login,¶øÊÇ/auth/loginµÄ»°£¬¾Íд$request->is('auth/login')
//²¢ÇÒÎÒÃÇÒªÔÚÇeÇo´¦ÀiºoÖ´ÐÐÆaÈÎÎñ£¬ÒoΪÎÒÃÇÐeÒª»ñÈ¡µ½Óû§µÄµÇ¼ÐÅÏ¢
$response = $next($request);
if (!Auth::check() && !$request->is('login')) {
return redirect('/login');
}
return $response;
}
}
Õa¸oÖмa¼þµÄ¹¦ÄÜÊÇ£¬Èç¹uÓÐÂÓɲuÉu£¬Ê×ÏÈʹÓÃAuth::check()
ÅжÏÓû§ÊÇñµÇ¼£¬Èç¹uûÓеǼµÄÌøתµ½µÇ¼ҳ¡£
½¨Ð´ºÃÁË£¬µ«ÊÇ»¹²»ÄÜʹÓã¬ÎÒÃÇÐeҪע²aÏÂÕa¸oÖмa¼þ£¬¸aeËß¿o¼ÜÎÒÃÇÕa¸oÖмa¼þдºÃÁË£¬¿ÉÒÔʹÓÃÁË£¬Ê¹ÓõÄ*¶Î§ÊÇÄÄÀi¡£
ÔÚapp/Http/
Ŀ¼ÏÂÓиoKernel.php
ÎļþÊÇ×¢²aÕa¸oÖмa¼þµÄ£¬Ò²¾ÍÊǸaeËß¿o¼Ü£¬ÎÒÃÇдºÃÁËÕa¸oÖмa¼þ¡£
¶øKernel.php
ÎļþÀiÓÐÁ½¸oÊý×eÊoÐÔ£¬Ò»¸o$middleware
±iʾȫ¾ÖʹÓÃ,Ò»¸o$routeMiddleware
±iʾ¿ÉÒÔÑ¡ÔñʹÓá£
È«¾ÖʹÓõÄÒa˼ÊÇ£¬²»¹ÜÄaÇeÇoÄĸoÒ³Ãae£¬¶¼»aÏÈÖ´ÐÐÕa¸oÖмa¼þ¡£
Ñ¡ÔñʹÓñiʾ£¬ÐeÒªÄĸoHTTPÇeÇo£¬ÒªÇoÖ´ÐÐÖмa¼þ£¬¾ÍÔÚÄĸoµØ*½Ö´ÐС£
ÕaÀiÿ¸oÒ³Ãae¶¼ÒªÇo±ØÐeµÇ¼µÄ»°£¬¿É¶¨ÊÇ×¢²aÒ»¸oÈ«¾ÖµÄ£¬ÔÚ$middleware
Êý×eÊoÐÔÀi¼ÓÈeÒ»Ìo
\App\Http\Middleware\CheckLoginMiddleware::class
×¢²aÏ£¬¾Í¿ÉÒÔʹÓÃÁË
>×¢Òa:Çe¼Çס£¬Èç¹u¶¨ÒaÈ«¾ÖµÄÒª¸ñÍaСÐÄ£¬±ÈÈçÉÏÃaeÎÒÃÇÒªÅųýµÇ¼ҳ£¬²»È»ÒoΪÓû§Ã»ÓеǼ£¬ËuÒÔÔÚÄĸoÒ³Ãae¶¼»aÖض¨Ïoµ½µÇ¼ҳ£¬µ±È»Ò²°uÀ¨µÇ½ҳ
6. Öмa¼þ¨CÌØÊaÒ³ÃaeÐeÒªÑeÖ¤Óû§×e
ÏÖÔÚÊǽøÐÐÓû§È¨ÏÞÒ³ÃaeµÄÏÞÖÆ£¬Í¬ÑuÎÒÃÇÒ²ÒªÖØд´½¨Ò»¸oÖмa¼þ
ʹÓÃphp artisan make:middleware CheckGroupMiddleware
´´½¨Ò»¸oеÄÖмa¼þ£¬ÓÃÀ´ÅжÏÕa¸oÓû§ÊÇ*ñÂu×aÕa¸oȨÏÞ
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class CheckGroupMiddleware
{
public function handle($request, Closure $next)
{
$user = Auth::user();
if ($user->groupid != 1) {
return redirect('/');
}
return $next($request);
}
}
ÕaÀiÎÒÃÇ»¹ÊÇͨ¹ýAuth::user()
À´»ñÈ¡µ½Óû§µÄÐÅÏ¢£¬È»ºoÅжÏÓû§µÄ×e£¬²»ÊoÓÚ³¬¼¶¹ÜÀiÔ±¾ÍÌøµ½Ê×Ò³¡£
È»ºoÎÒÃÇÔÚµ½app/Http/
Ŀ¼ÏÂÓиoKernel.php
ÎļþÊÇ×¢²aÕa¸oÖмa¼þµÄ£¬Õa´ÎÎÒÃÇ×¢²aΪ¿ÉÒÔÑ¡ÔñµÄÖмa¼þ¡£
Õa¸oÖмa¼þÒoΪÊÇ¿ÉÒÔÑ¡ÔñµÄ£¬ËuÒÔÎÒÃÇ»¹ÐeÒª¸øËuÆð¸o±ðÃu,ÔÚ$routeMiddleware
Êý×eÊoÐÔÀi¼ÓÈçÒ»Ìo
'user.group' => \App\Http\Middleware\CheckGroupMiddleware::class
´´½¨Ò»¸o¿ÉÒÔʹÓÃusergroup
Õa¸oÃu×ÖʹÓõÄÖмa¼þ¡£
´´½¨ºÃºo£¬ÎÒÃÇ¿ÉÒÔÑ¡ÔñÔÚÄÄÀiʹÓã¬Ò»¸oÊÇÔÚrouter.php
µÄÂ*ÓÉÎļþÀi¼ÓÈe£¬Ò»¸oÊÇÔÚcontroller
ÀiʹÓÃ
ÔÚrouter.phpÎļþÀiʹÓÃ
Route::get('/', ['middleware' => ['user.group'], function () {
//
}]);
ÔÚ¿ØÖÆÆ÷ÄÚʹÓÃ
$this->middleware('user.group');
ÕaÀiÎÒÃÇÑ¡ÔñÔÚÂ*ÓÉÀiÌi¼ÓÖмa¼þ¡£ÈÃ×¢²aÒ³ÃaeÖ»ÄÜÊdz¬¼¶¹ÜÀiÔ±²Å¿ÉÒÔ×¢²a
Route::get('register', 'UserController@getRegister')->middleware('user.group');
Route::post('register', 'UserController@postRegister')->middleware('user.group');
ÎÒÃÇÄ¿Ç°Ö»ÓÐÁ½¸oÂÓÉÒªÅжÏȨÏÞ£¬ËuÒÔʹÓÃÁËÁ´Ê½µÄд¨£¬µ±È»ÄaÒ²¿ÉÒÔ°´ÕÕÊÖ²aÀiÉÏʹÓÃ×eµÄ½Ê½£¬×eµÄ½Ê½¸uΪÓÅÑÅ¡£
µ±È»Èç¹uÄaµÄÕu¸o¿ØÖÆÆ÷ÄڵĽ¨¶¼ÐeÒªÖмa¼þ½øÐÐÑeÖ¤¹ýÂ˵Ļ°£¬ÄaÒ²¿ÉÒÔ´´½¨×eµÄÐÎʽ£¬Ò²¿ÉÒÔÖ±½ÓÔÚ¿ØÖÆÆ÷ÄÚʹÓÃ__construct
½¨,ÈÃÿ´ÎÇeÇoÕa¸o¿ØÖÆÆ÷ʱ£¬ÏÈÖ´ÐÐÖмa¼þ
class MyController extends Controller
{
public function __construct()
{
$this->middleware('user.group');
}
public function index()
{
return view('my.index');
}
}
×ܽa
ÒÔÉϾÍÊÇLaravel×¢²aÖع¹µÄÈ«²¿ÄÚÈÝ£¬Ï£Íu±¾ÎĵÄÄÚÈݶԴo¼ÒʹÓÃLaravelÄÜÓÐËu°iÖu£¬Èç¹uÓÐÒÉÎÊ»¶Ó´o¼ÒÁoÑÔÌÖÂÛ£¬Ð¡±a»a¾¡¿i¸ø´o¼Ò»Ø¸´¡£