我在管理员控制器中创建了以下方法:
public function index() {
// $recentBlogPost = DB::table('Admin')->get();
// Auth::logout();
if (!(Auth::check())) {
return Redirect::to('login');
}
$tags = DB::table('Tags')->get();
/* convert Object to array */
$tagsArray = array();
foreach($tags as $tag) {
$tagsArray[$tag->tag] = $tag->tag;
}
$tagsArray = json_decode(json_encode($tagsArray) , TRUE);
return view('admin.index')->with('tags' , $tagsArray);
}
现在我看到以下代码行
(Auth::check())
我从这里的laravel文档中获取了此代码 - > https://laravel.com/docs/5.0/authentication
我真的很想知道那个类或方法真的是在引擎盖下做什么,我把这个文件拉到了引擎盖下的代码中。“
答案 0 :(得分:1)
// Illuminate\Auth\GuardHelpers
/**
* Determine if the current user is authenticated.
*
* @return bool
*/
public function check()
{
return ! is_null($this->user());
}
如果您想了解$this->user()
:
// Illuminate\Auth\SessionGuard
/**
* Get the currently authenticated user.
*
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
{
if ($this->loggedOut) {
return;
}
// If we've already retrieved the user for the current request we can just
// return it back immediately. We do not want to fetch the user data on
// every call to this method because that would be tremendously slow.
if (! is_null($this->user)) {
return $this->user;
}
$id = $this->session->get($this->getName());
// First we will try to load the user using the identifier in the session if
// one exists. Otherwise we will check for a "remember me" cookie in this
// request, and if one exists, attempt to retrieve the user using that.
$user = null;
if (! is_null($id)) {
if ($user = $this->provider->retrieveById($id)) {
$this->fireAuthenticatedEvent($user);
}
}
// If the user is null, but we decrypt a "recaller" cookie we can attempt to
// pull the user data on that cookie which serves as a remember cookie on
// the application. Once we have a user we can return it to the caller.
$recaller = $this->getRecaller();
if (is_null($user) && ! is_null($recaller)) {
$user = $this->getUserByRecaller($recaller);
if ($user) {
$this->updateSession($user->getAuthIdentifier());
$this->fireLoginEvent($user, true);
}
}
return $this->user = $user;
}
Illuminate\Support\Facades\Auth
,但没有检查方法。Illuminate\Support\Facades\Auth
扩展Illuminate\Support\Facades\Facade
Illuminate\Support\Facades\Facade
有一个神奇的__callStatic()
方法__callStatic
执行'{1}}方法'auth'实例
check
Illuminate\Auth\AuthServiceProvider::registerAuthenticator()
方法__call
执行'检查'方法在'guard'实例上
Illuminate\Auth\AuthManager::__call()
根据您的Illuminate\Auth\AuthManager::guard()
guard
configs/auth.php
- guard
(session
)Illuminate\Auth\SessionGuard
没有Illuminate\Auth\SessionGuard
方法,但它使用check
特征Illuminate\Auth\GuardHelpers
有Illuminate\Auth\GuardHelpers
方法换句话说,Auth facade:
check
方法答案 1 :(得分:0)
要了解其工作原理,最好查看check()
方法源代码:
public function check()
{
return ! is_null($this->user());
}
所以它只是检查用户实例是否存在并返回true
或false
。