ʾÀýÏê½âLaravelµÄ×¢²áÖع¹

5年以前  |  阅读数:1046 次  |  编程语言:PHP 

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¼Ò»Ø¸´¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例