幽灵资源网 Design By www.bzswh.com

Laravel 自动授权讲解

看到这部分文档,经常看见的一句话就是php artisan make:auth,经常好奇这段代码到底干了什么,现在就来扒一扒。

路由

路由文件中会新加入以下内容:

Auth::routes();
Route::get('/home','HomeController@index')->name('home');

首先先是Auth::route();,这句代码等于以下全部设置(文件位置是\Illuminate\Routing\Router.php):

/**
  * Register the typical authentication routes for an application.
  *
  * @return void
  */
 public function auth()
 {
  // Authentication Routes...
  $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
  $this->post('login', 'Auth\LoginController@login');
  $this->post('logout', 'Auth\LoginController@logout')->name('logout');

  // Registration Routes...
  $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
  $this->post('register', 'Auth\RegisterController@register');

  // Password Reset Routes...
  $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
  $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
  $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
  $this->post('password/reset', 'Auth\ResetPasswordController@reset');
 }

这一部分先讲注册,首先,可以看到登录(login)的路由指向的是Auth\LoginController@showLoginForm,这个控制器是app\Http\Auth\LoginController.php,这里贴一下他的代码:

class LoginController extends Controller
{
 /*
 |--------------------------------------------------------------------------
 | Login Controller
 |--------------------------------------------------------------------------
 |
 | This controller handles authenticating users for the application and
 | redirecting them to your home screen. The controller uses a trait
 | to conveniently provide its functionality to your applications.
 |
 */

 use AuthenticatesUsers;

 /**
  * Where to redirect users after login.
  *
  * @var string
  */
 protected $redirectTo = '/home';

 /**
  * Create a new controller instance.
  *
  * @return void
  */
 public function __construct()
 {
  $this->middleware('guest')->except('logout');
 }
}

而其中并没有设置showLoginForm方法,该方法被保存在trait AuthenticatesUsers中,该方法的代码如下:

public function showLoginForm()
 {
  return view('auth.login');
 }

就是返回一个视图,下面我们来看该视图:

<form class="form-horizontal" method="POST" action="{{ route('login') }}">
</form>

而其中最重要的就是看这个表单被提交到了哪里,结合上面的路由表,可以看到是

public function login(Request $request)
 {
  $this->validateLogin($request);
  /**
  *
  protected function validateLogin(Request $request)
 {
  $this->validate($request, [
   $this->username() => 'required|string',
   'password' => 'required|string',
  ]);
 }
  其中 $this->username() 就是 return 'email';
  **/
  // 限制请求次数,防止暴力破解的
  if ($this->hasTooManyLoginAttempts($request)) {
   $this->fireLockoutEvent($request);

   return $this->sendLockoutResponse($request);
  }
  /**
  // 关于 attempt 的介绍可以看我上一篇博客
  protected function attemptLogin(Request $request)
 {
  return $this->guard()->attempt(
   $this->credentials($request), $request->has('remember')
  );
 }
 **/
  // 如果验证通过的话
  if ($this->attemptLogin($request)) {
   return $this->sendLoginResponse($request);
  }
  // 否则的话增加验证的统计次数
  $this->incrementLoginAttempts($request);
  // 返回错误信息
  return $this->sendFailedLoginResponse($request);
 }

可以看到验证的重点还是Auth::attempt()函数,而且默认是使用email进行验证。

退出操作的代码如下:

public function logout(Request $request)
 {
  $this->guard()->logout();

  $request->session()->invalidate();

  return redirect('/');
 }

$this->guard()的代码如下:

protected function guard()
 {
  return Auth::guard();
 }

logout的具体的执行代码如下,别问我怎么找到的,PHPStorm的全项目文本搜索不解释:\Illuminate\Auth\SessionGuard.php:

public function logout()
 {
  $user = $this->user();

  $this->clearUserDataFromStorage();

  if (! is_null($this->user)) {
   $this->cycleRememberToken($user);
  }

  if (isset($this->events)) {
   $this->events->dispatch(new Events\Logout($user));
  }

  // Once we have fired the logout event we will clear the users out of memory
  // so they are no longer available as the user is no longer considered as
  // being signed into this application and should not be available here.
  $this->user = null;

  $this->loggedOut = true;
 }

其中牵扯很多,那么我换种角度考虑,假设我们不考虑logout()的具体实现,而是思考如何制作自己的退出设置,那么该如何修改源码呢?好像直接修改成下面的形式就可以了:

public function logout(Request $request)
 {
  Auth::guard()->logout();

  $request->session()->invalidate();
  // 自定义重定向地址
  return redirect('/');
 }

其中的很多内容都跟我们的设置无关,全自动的调用,所以我们的退出按钮就只需要运行上述代码即可。本人请测有效。

以上这篇Laravel 自动生成验证的实例分析:login / logout就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
Laravel,验证,login,logout

幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。