Laravel,控制器中的Auth :: user() [英] Laravel, Auth::user() in controller
问题描述
Laravel框架-为什么我不能在laravel项目的控制器中使用Auth :: user()(以查看用户是否已登录).会话未连接到控制器吗?
Laravel framework- Why aren't I able to use Auth::user(), (to see if user has been logged in) in the controller of the laravel project. Is the Session not connected to the controller?
HomeController.php
HomeController.php
public function isauthorized(){
if (Auth::user()){
return View::make('home.basic')
->with('basic1', 'Authorized');
}else{
return View::make('home.basic')
->with('basic1', 'Not Authorized');
}
}
我的登录系统正在运行,并且登录时始终会设置会话.当我在View或routes.php中调用函数Auth :: user()时,它会成功.但是,当我在控制器中调用该函数时,它是错误的.请帮助.
My login system is working and the session is always set when logged in. When I call the function Auth::user() in the View or in routes.php, it is succesfull. But when I call the function in the controller, it is false. Help please.
推荐答案
问题是您尚未包括Auth外观.
The problem is that you haven't included the Auth facade.
在您的HomeController.php中执行:
In your HomeController.php do:
use Illuminate\Support\Facades\Auth;
这篇关于Laravel,控制器中的Auth :: user()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!