LARAVEL學習 DAY 24 新的開始 – Authentication(二)
前言
老司機飆車中
前言
老司機飆車中
正文
Laravel提供了很棒的Facade
然後我們的Auth就是這樣用
use Auth;
class XXXClass
{
function xxx()
{
return Auth::user();
}
}
這邊可以取到已登入的user 如果沒有 就是null
還有其它的 比如說Auth::id()可以取到id
然後如果不想要use Auth;的話也可以auth()->user()
雖然我不知道這是什麼妖術…
然後這個也可以取 $request->user()
真的是一堆黑科技 還有這個 auth()->check() 這個就是簡單的true false 看有沒有登入
public function authenticate()
{
if (Auth::attempt([’email’ => $email, ‘password’ => $password])) {
return redirect()->intended(‘dashboard’);
}
}
然後也可以這樣自幹登入… 如果有必要的話啦
然後這些傢伙
Auth::login($user);
Auth::loginUsingId(1);
後面的$user必須是一個instance 這東西可以拿來做測試啊什麼的
不過我沒什麼用到就是了
結語
結語打成節育… 又不是非洲人
转载请注明:XAMPP中文组官网 » LARAVEL學習 DAY 24 新的開始 – Authentication(二)