Laravel 5:所有控制器中的全局 Auth $user 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32850376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5: Global Auth $user variable in all controllers
提问by Chad Priddle
In my BaseController I have this:
在我的 BaseController 中,我有这个:
public function __construct()
{
$user = Auth::user();
View::share('user', $user);
}
I can access the $user array throughout all of my views but what if I want it to be a global variable available in all of my Controllers?
我可以在所有视图中访问 $user 数组,但是如果我希望它成为所有控制器中可用的全局变量怎么办?
I have the following working code:
我有以下工作代码:
use Auth;
use DB;
class UsersController extends BaseController
{
public function index()
{
$user = Auth::user();
$users = DB::table('users')->where('user_id', $user->user_id)->get();
return view('pages.users.index', compact('users');
}
}
I want the $user variable available in all of my controllers and within all of the public functions within each on the controllers but I don't want to have to redeclare "use Auth;" and "$user = Auth::user();"
我希望 $user 变量在我的所有控制器中以及每个控制器上的所有公共函数中都可用,但我不想重新声明“使用身份验证;” 和“$user = Auth::user();”
I tried adding it to a __construct in the BaseController but that didn't seem to work, Undefined $user variable error. Any thoughts? I know it's possible.
我尝试将它添加到 BaseController 中的 __construct 中,但这似乎不起作用,未定义 $user 变量错误。有什么想法吗?我知道这是可能的。
采纳答案by Gras Double
Has shown in other answers, you can:
已在其他答案中显示,您可以:
- set it as a property of your base controller (I don't recommend this, it just adds clutter)
- create a custom helper (see below)
- just do
\Auth::user()
- 将其设置为基本控制器的属性(我不推荐这样做,它只会增加混乱)
- 创建自定义助手(见下文)
- 做就是了
\Auth::user()
Another technique, you can use auth()
helper:
另一种技术,您可以使用auth()
助手:
auth()->user()
It is even documented.
它甚至被记录在案。
According to this PR, there is no plan to add an user()
helper in Laravel.
根据这个 PR,没有计划user()
在 Laravel 中添加一个助手。
If you consider it is worth adding this helper to your project, here is the code:
如果您认为值得将这个助手添加到您的项目中,这里是代码:
function user()
{
// short and sweet
return auth()->user();
// alternative, saves one function call!!
return app('Illuminate\Contracts\Auth\Guard')->user();
}
回答by user1669496
I do not see why something like this would not work:
我不明白为什么这样的事情行不通:
public function __construct()
{
$this->user = \Auth::user();
\View::share('user', $this->user);
}
This way it's available to all your views and as long as your controllers are extending BaseController
, it's available in all your controllers as well with $this->user
.
这样它就可用于您的所有视图,并且只要您的控制器正在扩展BaseController
,它也可以在您的所有控制器中使用$this->user
.
回答by Gal
I don't see whats the issue with using Auth::user()
, this class is using singleton pattern which means that if the Auth::user()
is already initialized it won't check again against the database especially when most of the code shouldn't even be inside a controller so basically you would still need to do Auth::user()
inside your services, models, form requests, etc..
我不明白 using 有什么问题Auth::user()
,这个类使用的是单例模式,这意味着如果Auth::user()
已经初始化它不会再次检查数据库,尤其是当大多数代码甚至不应该在控制器中时,基本上您仍然需要Auth::user()
在您的服务、模型、表单请求等中进行操作。
abstract class Controller extends BaseController {
protected $user;
use ValidatesRequests;
public function ___construct(){
$this->user = \Auth::user();
}
}
回答by Mustafa Ehsan Alokozay
Why don't you use a helper function. It won't give you a $user variable, but it can give you a more cleaner way to accomplish this.
为什么不使用辅助函数。它不会为您提供 $user 变量,但可以为您提供更简洁的方法来完成此操作。
In your helpers.phpadd a function:
在你的helpers.php添加一个函数:
function getUser(){
return Auth::user();
}
In all other places (controller, views, models) call: getUser()
. That's it.
在所有其他地方(控制器,视图,模型)拨打:getUser()
。就是这样。
If you don't have any helper file for your functions yet. You can follow this linkto create one.
如果您的函数还没有任何帮助文件。您可以按照此链接创建一个。